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

Does the following script require something extra for it to work?

Asked by 9 years ago

I'm trying to make a GUI that includes multiple small functions(such as a button that lets you open/close another frame and a reset button). I'm using one single LocalScript, whos parent is just ScreenGui, and I have this:

sp=Script.Parent
plr=game.Players.LocalPlayer
char=plr.Character
sp.ResetButton.MouseButton1Click:connect(function()
    hum=char:findFirstChild("Humanoid")
    hum.Health=0
end)

For some reason, this works perfectly fine when the localscript is in the button itself, but it doesn't work when it's outside of the button. For convenience sake, I'd rather be able to manage all the buttons in one script. I remember I did something like this last year and it would work, but of course, so much has changed since then, however I don't know if it's something I'm doing wrong or does the code require something extra for it to function again?

0
line 5 -- FindFirstChild lomo0987 250 — 9y
0
I've used findFirstChild in a localscript inside the button before, and it still worked, I don't think that made a difference qq Devotional 210 — 9y
0
Sorry, I don't think that was me that downvoted you though, this is the first question I asked and I tried to upvote it but it says that I need at least one reputation first. Devotional 210 — 9y
0
Must have been someone else. 2eggnog 981 — 9y
0
Yeah, but I thank you so much! It did work, and I appreciate it! Devotional 210 — 9y

1 answer

Log in to vote
2
Answered by
2eggnog 981 Moderation Voter
9 years ago

The script actually runs before the gui is fully added to the PlayerGui. You can fix that by using WaitForChild.

sp=script.Parent
plr=game.Players.LocalPlayer
char=plr.Character
sp:WaitForChild("ResetButton").MouseButton1Click:connect(function()
    hum=char:findFirstChild("Humanoid")
    hum.Health=0
end)
Ad

Answer this question