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

Help with ontouched?

I have this script, its meant to detect if the localplayer touches it and does something if they do.

local player = game.Players.LocalPlayer
script.Parent.Touched:connect(function (hit)
if hit.Parent == player then
player.StarterGui.examplegui:Destroy()
end
end)

however it does nothing, please help as in a rush to complete the script!

1 answer

✓ Accepted answer

The player is just a player object, not the item touching it. The item that is touching it is the player's character. You need to reference the character in the script, otherwise it will never work.

local player = game.Players.LocalPlayer
repeat wait() until player.Character

script.Parent.Touched:connect(function (hit)
	if hit.Parent == player.Character then
		player.PlayerGui.examplegui:Destroy() --Player does not have a StarterGui, but it's own PlayerGui.
	end
end)

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