Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

5 comments

0

line 5 -- FindFirstChild

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

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.

0

Must have been someone else.

0

Yeah, but I thank you so much! It did work, and I appreciate it!

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.