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

onTouch script doesn't work? [Solved]

This doesn't work for some reason. No output. I think it's because I am using "LocalPlayer" to find the player that touched the part. Any ways to fix this?

function onTouch()
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Script.Disabled = false
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
	game.Players.LocalPlayer.PlayerGui.ScreenGui.ImageLabel.Visible = true
	game.Players.LocalPlayer.PlayerGui.End:Play()
end

script.Parent.Touched:connect(onTouch)

1 comment

0

It does work in play solo on the edit mode. But it doesn't work when I play it as a server.

4 answers

✓ Accepted answer
function touch(plr)
player = game.Players:FindFirstChild(plr.Name)
-- Code..
end

What I do to get the player that has touched a brick is this..

function touch(plr)
player = game.Players:FindFirstChild(plr.Name)
-- Code..
end

Local Player only works in local scripts that are located somewhere inside the player such as PlayerGUI (StarterGUI) or the BackPack. (i don't know the names for that exactly, I would have to check.)

2 comments

1

Thanks for advice.

0

no problem :D You might need a check in it as well.. Just to make sure it's a player, like if player then ... end

Is this in a Script or a LocalScript? Now, rather than using LocalPlayer, find the player who hit the part.

function onTouch(hit)
	if hit and hit.Parent.Character then
		hit.Parent.PlayerGui.ScreenGui.Script.Disabled = false
		hit.Parent.PlayerGui.ScreenGui.Frame.Visible - true
		hit.Parent.PlayerGui.ScreenGui.ImageLabel.Visible = true
		hit.Parent.Playergui.End:Play()
	end
end

script.Parent.Touched:connect(onTouch)

The only reason the code I provided wouldn't work is if I got the hierarchy of the hit wrong, which is possible.

Fixed it myself:

function onTouch(plr)
	player = game.Players:GetPlayerFromCharacter(plr.Parent)
	player.PlayerGui.ScreenGui.Script.Disabled = false
	player.PlayerGui.ScreenGui.Frame.Visible = true
	player.PlayerGui.ScreenGui.ImageLabel.Visible = true
	player.PlayerGui.End:Play()
end

script.Parent.Touched:connect(onTouch)

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