Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why are the functions failing to be called?

Asked by 9 years ago

So this script is supposed to call on functions when certain values are made true. However, it doesn't work, and I have no idea why. Output has been saying the functions I am trying to call are nil. Can someone please help me?

Here's the script:

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

while wait() do
if Playing.Value == true then
if Done.Value == true then
End()
wait(3)
ReDo()
end
end
end

function End()
print('Game over')
end

function ReDo()
print('Restarting')
end
0
Check if the values are true. Instead of doing if Playing.Value == true then, do if Playing.Value then. Shortcuts! EzraNehemiah_TF2 3552 — 9y
0
Output said that the functions "End()" and "ReDo()" are nil. Forgot to add that. CoolJohnnyboy 121 — 9y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

That's because they are nil.


Your script is being read left to right, top to bottom. Therefore when you call the function, the function doesn't exist yet because you haven't created it. You will actually never create it, since you put it after a while that will loop forever. Even though you type out the code, that code will never be read nor will it be executed.

Just create the function before you get sucked into an endless loop.

--Tab your code correctly!

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

function End()
    print('Game over')
end

function ReDo()
    print('Restarting')
end

while wait() do
    if Playing.Value == true then
        if Done.Value == true then
            End()
            wait(3)
            ReDo()
        end
    end
end

Might as well combine the if statements and shorten the conditions;

--Tab your code correctly!

local Playing = script:WaitForChild("Playing")
local Done = script:WaitForChild("Done")

function End()
    print('Game over')
end

function ReDo()
    print('Restarting')
end

while wait() do
    if Playing.Value and Done.Value then
        End()
        wait(3)
        ReDo()
    end
end
0
Thanks! CoolJohnnyboy 121 — 9y
0
You could also shorten them even more by doing: local Playing, Done = script:WaitForChild("Playing"),script:WaitForChild("Done") EzraNehemiah_TF2 3552 — 9y
0
Yes, but using tuples like that leads to very unreadable and messy code. Perci1 4988 — 9y
Ad

Answer this question