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

attempt to index local 'Hit', a nil value?

script.Parent.MouseButton1Click:connect(function(Hit)
	pcall(function()
	local p = game.Players:GetPlayerFromCharacter(Hit.Parent):WaitForChild("Folder"):WaitForChild("Buns")
	if p.Value>= 2
		then print("Yay")
	end
	end)
end)

This script is attempting to check if a players "Buns" Value equals more then 2 or is equal to 2. There is no errors, but it does not function.

script.Parent.MouseButton1Click:connect(function(Hit)
	local p = game.Players:GetPlayerFromCharacter(Hit.Parent):WaitForChild("Folder"):WaitForChild("Buns")
	if p.Value>= 2
		then print("Yay")
	end
end)

This script does error. the only thing removed though is PCALL.

21:25:02.248 - Workspace.Part.SurfaceGui.Frame.Blue Pen.Script:2: attempt to index local 'Hit' (a nil value) 21:25:02.248 - Stack Begin 21:25:02.249 - Script 'Workspace.Part.SurfaceGui.Frame.Blue Pen.Script', Line 2 21:25:02.249 - Stack End

((And yes, The Buns is set to 2.))

1 answer

✓ Accepted answer

Surface Guis can not detect who has clicked them if they are in the workspace. To find out who clicked them, you must put the Surface Gui inside the StarterGui and Adornee it to the part in workspace. To do this, put a local script inside the Surface Gui and type,

script.Parent.Adornee = game.Workspace.Part --(or whatever your part name is)

Then inside your button, you can define a variable with game.Players.LocalPlayer and use it inside the MoudeButton1Click function as the player who clicked the button.

1 comment

0

Oh. Lovely. Welp im just gonna stick to screen gui's theenn

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