I have a (Corona SDK) project to load a series of scenes (composer) and it seems on 3 of my scenes that load newVideo objects, the scene:show is being fired several times and I can't understand why.
The calling code looks like this:
local btn2 = self:getObjectByTag("button2");
local function btn2_click(e)
if(e.phase == "ended") then
composer.gotoScene("video_paulprice");
end
end
btn2:addEventListener("touch", btn2_click);
The composer scene show method looks like this:
function scene:show( e )
local sceneGroup = self.view
local phase = e.phase
local halfscreenwidth=display.contentWidth/2;
local halfscreenheight=display.contentHeight/2
local bg = self:getObjectByTag("backgroundBox");
bg.x= display.contentWidth/2;
bg.y= display.contentHeight/2;
bg.xScale=0.5;
bg.yScale=0.5;
print("CM PHASE: " .. e.phase)
if (phase == "did") then
video = native.newVideo( display.contentCenterX+1, display.contentCenterY-40, 535, 390 );
local function videoListener( event )
print( "Event phase: " .. event.phase );
end
video:load( "videos/video_1.mp4", system.ResourceDirectory );
video:addEventListener( "video", videoListener );
video:play()
end
local btn1 = self:getObjectByTag("button1");
local function btn1_click(e)
display.remove(video)
composer.gotoScene("scene3");
end
btn1:addEventListener("tap", btn1_click);
local btn2 = self:getObjectByTag("button2");
local function btn2_click(e)
video:removeSelf()
composer.gotoScene("scene4");
end
btn2:addEventListener("tap", btn2_click);
end
My terminal, debugger output looks like this:
2014-12-08 10:37:29.756 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.757 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.852 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.852 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.852 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.852 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.852 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.853 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.853 Corona Simulator[4392:175603] CM PHASE: will
2014-12-08 10:37:29.885 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.885 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.885 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.885 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.885 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.886 Corona Simulator[4392:175603] CM PHASE: did
2014-12-08 10:37:29.886 Corona Simulator[4392:175603] CM PHASE: did
Other scenes in my project the show method is only being fired once. I'm using build: Version 2014.2494 (2014.11.5)
Any help would be hugely helpful!
Thanks
Chris.
After re-building the scenes manually, rather than using the compower UI in the latest build, the scenes now no longer fire show() / hide() / create() / destroy() many times - strange!
Maybe some sorts of weird bug in the composer ui? Have a thread working on this over on the corona SDK forums, will close this for now.
In short, my resolution was to recreate manually and position objects manually, rather than using the computer UI software in the latest build.
Related
I am getting the folowing error when running my script:
Script timeout: exhausted allowed execution time
Here is my script:
-- Define variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 55
local reward = 25
-- Game loop
while true do
-- <game loop body here>
end
Why is this happening and how can I fix it?
As ewong was hinting at in the comments, your issue is with the line while true do end. It's unclear from your code sample why you are doing this, but I'll assume that when you said that this isn't the full code, that you removed the contents of the loop itself.
In an engine like Roblox, each script has a limited amount of time to complete its work before the engine needs to move on. If it cannot finish the work, and never yields, then the engine will kill it to prevent it from locking up the rest of the game.
The most simple fix to make this error go away is to allow your loop to yield. So adding wait() inside the loop will do the trick.
while true do
-- do your game logic every tick here
wait()
end
However, the better way to have code execute every tick is to listen to the RunService.Heartbeat signal.
game.RunService.Heartbeat:Connect( function(timeStep)
-- do your game logic here
end)
U missing a wait in ur while loop, this causes to overflow the server/client.
I am making a game. I am trying to make a script that will prompt the player to buy a gamepass when they click a button on a SurfaceGUI. However, the function isn't executing, and I don't know why.
If it helps, it is in a LocalScript.
script.Parent.MouseButton1Click:Connect(function()
local mps = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
print("vars")
mps:PromptGamePassPurchase(player, 10772382)
print("prompt")
end)
The issue is that this is a LocalScript, and LocalScripts only execute from a few locations. According to the docs :
A LocalScript will only run Lua code if it is a descendant of one of the following objects:
A Player’s Backpack, such as a child of a Tool
A Player’s character model
A Player’s PlayerGui
A Player’s PlayerScripts.
The ReplicatedFirst service
So to fix your issue, convert this LocalScript to a Script, and listen for a player to join to get access to the player object.
local mps = game:GetService("MarketplaceService")
local ps = game:GetService("Players")
ps.PlayerAdded:Connect(function(player)
script.Parent.MouseButton1Click:Connect(function()
print("vars")
mps:PromptGamePassPurchase(player, 10772382)
print("prompt")
end)
end)
I do not know why your current script is not working however, here is a script I use that works well(just change the ID value to your id
local id = 6604880
script.Parent.MouseButton1Click:Connect(function()
game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer,id)
end)
I have been following many tutorials that use:
game.Players.LocalPlayer.Character
However every time I try to run this I get an error saying:
Players.icrann.PlayerScripts.Script:2: attempt to index field 'Character' (a nil value)
I am trying to use this code to find the HumanoidRootPart so that I can check the position of the player. game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
And also enable the player to run, even if I try use scripts from the toolbox it still wont work. game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 25
In every case it brings up the same error.
I also included a screenshot of the Players folder in the Explorer.
Thank's in advance for any help.
I believe you have a timing issue. PlayerScripts execute when the player joins, and it's possible that your Character hasn't loaded yet by the time the script executes.
Move the LocalScript into CharacterScripts, and that will cause the script to fire after the Character has loaded.
When your code runs, the player character is not loaded into the game. So you need to add wait.
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- setting speed
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
(Local Script Part)
If it was a LocalScript and not a Script u can use
If those above didn't work then you can try:
repeat wait() until game:IsLoaded() and game:GetService("Players").LocalPlayer.Character on top of the script so it will just wait for the player's character.
(Server Sided Script)
If it was a Script and not a Local Script u can use
for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
if not plr:IsA("Player") then return end
repeat wait(1) until game:IsLoaded() and plr.Character
end
What I'm trying to do: I want to have a tree and every few seconds an Apple falls down from that tree. Player can "pick up" that Apple. If more players are in the game, player who picks up the most apples, wins.
What I have: I have a tree and apples are falling down. Until here it works perfect. Player can pick up an apple - if he touches the apple by his foot, apple is destroyed and player gets 1 point. Still OK.
What is wrong: If more players join the game, it looks like every player can see his own (local) apple. So if Player1 picks up an apple, apple is destroyed - but only for him :( all other players can see that apple still there and they can pick it up too. If I test-run the game with 2 players, in the server-window I can see that apple still there, even after all players picked it up. So the server has it's own instance apparently.
But I want just one global apple.
Application is like this:
I have an apple in the Workspace. Every few seconds I clone it in the script (not local script, but Script) which is under the AppleTree model in Workspace:
function GrowNewApple()
local newApplePos = GetRandomPlace()
local appleTemplate = workspace.apples.prototype
local newApple = appleTemplate:Clone()
newApple.Parent = appleTemplate.Parent
newApple.Name = "apple"
newApple.Position = newApplePos
end
In StarterPlayer / StarterPlayerScripts I have a localscript with this:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
character:WaitForChild("LeftFoot")
character.LeftFoot.Touched:Connect( PickUpApple )
And finally my PickUpApple function looks like this:
function PickUpApple( touchObject )
if touchObject:IsDescendantOf(workspace.apples) then
touchObject:Destroy()
end
end
Any ideas please?
Is it because the PickUpApple() is called from LocalScript? Is it possible that this LocalScript is sending local touchObject into this function?
I have no idea how to do this. Thanks guys.
Deleting the apple from a local script will only delete it for the client, To prevent this, Try it so the apple gets deleted by a server side script, You have 2 options:
1, Make the script a server side script and make sure it's compatible for the server.
2, Make a remote event which is fired once the local script detects the local player touching an apple, And make sure the remote event is connected to a function that deletes the apple and gives the player a point, Should be a server script, To do that:
1, Create a RemoteEvent (Make sure it's a RemoteEvent not a RemoteFunction!) in the ReplicatedStorage and rename it to "PickupApple".
2, Change the local script to:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local event = game.ReplicatedStorage:WaitForChild("PickupApple")
local apples = workspace:WaitForChild("apples") -- Using WaitForChild() function to prevent errors for requesting the object before it loads
character:WaitForChild("LeftFoot")
character.LeftFoot.Touched:Connect(function(touchObject)
if touchObject:IsDescendantOf(apples) then
event:FireServer(touchObject)
end
end)
3, Create a script (Not a LocalScript!) in the ServerScriptService, And put this:
game.ReplicatedStorage.PickupApple.OnServerEvent:Connect(function(player, item)
if item:IsDescendantOf(workspace.apples) then
item:Destroy()
-- Add here any extra code such as giving points, etc
end
end)
OK, problem solved.
The problem was that Touched event was fired on Local Player parts (feet, legs). This has sent the local instance of the apple to the Touched Event Handler.
Now I removed this:
character.LeftFoot.Touched:Connect( PickUpApple )
and instead of firing Touched on player foot I moved it to the Apple part and now I'm firing Touched event on that Apple part.
apple.Touched:Connect(PickUpApple)
And it works. While Apple part sends to the Touched Event Handler player's foot which is OK - I don't need to destroy it - I can destroy Apple now.
I have to say I moved whole function PickUpApple() into the apple part also so I have direct access to the apple part itself.
I'm working on a little project, using an ESP8266 with NodeMCU and Lua. I suspect I've found a bug, but since I'm new to Lua (and the other two too!), I'm hoping for some help in confirming if I'm right, or if I've missed something (more likely!).
The NodeMCU firmware contains a built-in SNTP client module which updates the synchronised time to a system clock (rtctime module). The success callback function seems to get called when (or possibly before) the NTP sync fails. This happens for example if the wifi is not connected, or sometimes on the first sync attempt after boot (with wifi connected). According to the doco, rtctime.get() returns zero if current time is not available; this is the result I get, further showing that the NTP sync hasn't been successful. I can't work out why the success function is being called at this point, in advance of, or instead of the failure function (as I would have expected).
The sntp module I'm referring to is here - unfortunately, the C source code was a bit over my head: https://nodemcu.readthedocs.io/en/master/en/modules/sntp/
My (minimal) code:
-- Define callback function for ntp sync success
function ntpSyncSuccess (sec, usec, server, info)
print('SNTP time sync successful!')
print("rtctime.get() returns: ", rtctime.get())
end
-- Configure and start NTP time sync with auto repeat enabled
sntp.sync("0.au.pool.ntp.org",
ntpSyncSuccess(sec, usec, server, info), --success callback
function() -- error callback
print('SNTP time sync failed!')
end,
1 -- enable autorepeat (SNTP sync every 1000 seconds (~17 min))
)
Serial output result when I boot the device and run the code (Note the 2nd and 3rd last lines):
NodeMCU custom build by frightanic.com
branch: master
commit: 11592951b90707cdcb6d751876170bf4da82850d
SSL: false
modules: cron,file,gpio,i2c,net,node,rotary,rtctime,sntp,struct,tmr,uart,wifi
build created on 2019-01-16 03:11
powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)
lua: cannot open init.lua
> print(uart.setup(0, 115200, 8, 0, 1, 1 ))
115200
> dofile("ntpTest.lua")
SNTP time sync successful!
rtctime.get() returns: 0 0 0
> SNTP time sync failed!
Just so that we can "close" this Q here (once you accept the answer). It has to be a function reference rather than a function invocation.
sntp.sync("0.au.pool.ntp.org",
ntpSyncSuccess, -- no (), no parameters
function() -- error callback