Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

onTouch script doesn't work? [Solved]

Asked by
RAYAN1565 691 Moderation Voter
10 years ago

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)
0
It does work in play solo on the edit mode. But it doesn't work when I play it as a server. RAYAN1565 691 — 10y

4 answers

Log in to vote
1
Answered by 6 years ago
function touch(plr)
player = game.Players:FindFirstChild(plr.Name)
-- Code..
end
Ad
Log in to vote
0
Answered by
lomo0987 250 Moderation Voter
10 years ago

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.)

1
Thanks for advice. RAYAN1565 691 — 10y
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 lomo0987 250 — 10y
Log in to vote
0
Answered by 10 years ago

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.

Log in to vote
0
Answered by
RAYAN1565 691 Moderation Voter
10 years ago

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)

Answer this question