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

Why can't I change something in PlayerGui?

Asked by 9 years ago

I need to change the text of a TextLabel to the word "HI" when a part is touched. It won't work, though. The TextLabel is in a ScreenGui in StarterGui. This script is in a part in the Workspace. Can someone tell me what I'm doing wrong?

script.Parent.Touched:connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        script.Parent:Destroy()
        game.Player.LocalPlayer.PlayerGui.ScreenGui.Total.Text = "HI"
    end
end)
0
Is it in a LocalScript? Ryzox 220 — 9y

1 answer

Log in to vote
2
Answered by
Ryzox 220 Moderation Voter
9 years ago

Use GetPlayerFromCharacter to get the person who touched the part's player

script.Parent.Touched:connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            player.PlayerGui.ScreenGui.Total.Text = "HI"
            script.Parent:Destroy()
        end
    end
end)
Ad

Answer this question