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

Script wont print "congrats" when the player clicks a gui and detects a humanoid? (error l2)

my friend told me that hit is not a valid argument of MouseButton1Click. That means hit is nil because the event MouseButton1Click does not return any arguments.

but he didnt tell me how to fix it, please help.

	script.Parent.MouseButton1Click:Connect(function(hit)	 
	    local h = hit.Parent.Parent.Parent.Workspace:FindFirstChild("Humanoid")
	 
    if h then
	 
	 
	        print("Congrats!")
	 
	 
 
	    end

Error: https://gyazo.com/df380ba3e678d975f5430ae804ce65a8

hierarchy: https://gyazo.com/a89005bd2f1457874f86a130fcb5b4e1

6 comments

0

Im not quite sure what your trying to achieve on line 2.

0

Im trying to detect if there is a humanoid in workspace

0

(on click)

0

I posted the answer back on your other post.

0

Make it a LocalScript, if you're doing anything GUI wise, make it Local. Then write, script.Parent.MouseButton1Click:Connect(function() if game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") then print("Congrats") end end)

0

@Player1_Joined is correct with his answer.

3 answers

You should not use a server script for a screen gui. Instead, use local scripts since it'll work in-game. As for the "hit" issue, normally, people use hit for a touched event.

For example:

script.Parent.Touched:Connect(function(hit)
if hit then --this checks to see whether the part was hit
print("part was hit!")
local Humanoid = hit.Parent.FindFirstChild("Humanoid") 
if Humanoid and Humanoid ~= nil then --this checks to see if a PLAYER has hit the part
print("player hit part!")
end
end
end)

In a local script, for a text button, all you need to do is:

script.Parent.MouseButton1Click:Connect(function() --You don't need to check if a player pressed the button
print("congrats")
end)

You seem to misunderstand how StarterPlayerGui works. StarterPlayerGui never shows up ingame, instead it copies its descendants into a PlayerGui folder inside of every Player. So there is no reason to use hit.

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