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

Using Touched and TouchEnded to change value in GUI?

Asked by 7 years ago
Edited 7 years ago

Hello, I was building a brick(CanCollideFalse) where if you step in, a value becomes true, and if you step out, the value becomes false. The below script works perfectly in Studio Test Mode:

script.Parent.Touched:connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then
        Player.PlayerGui.ComputerGui.Battery.change.charging.Value = true
    end
end)

script.Parent.TouchEnded:connect(function(out)
local Player = game.Players:GetPlayerFromCharacter(out.Parent)
    if Player then
        Player.PlayerGui.ComputerGui.Battery.change.charging.Value = false
    end
end)

... however fails in game with an error stating that on Line 4 and 11: ComputerGui is not a valid member of PlayerGui (when ComputerGui is clearly a child of PlayerGui.)

Maybe it's an issue with Player not being the parent of PlayerGui?

Here's a pic of my StarterGui: http://tinypic.com/r/2qceb7o/9

0
Is this a localscript? Picture of your startergui? Azmidium 388 — 7y
0
This is a script in a part in workspace. hipenguinflip 30 — 7y

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Scripts can't acces PlayerUI. You'll have to fire a RemoteEvent, which a localscript will trigger from. This way, you can tell a specific client, to do something, so the server doesn't have to do it. (You most likely want to handle anything UI related to the client.)

Example:

--script
Part.Touched:Connect(function(a)
    --Check if player
    if game.Players:GetPlayerFromCharacter(a.Parent) then
    --The "true" here, is "state" in the localscript. (First argument)
        game.ReplicatedStorage.RemoteEvent:FireClient(game.Players[a.Parent.Name], true)
    end
end)

Part.TouchEnded:Connect(function(a)
    --Check if player
    if game.Players:GetPlayerFromCharacter(a.Parent) then
    --The "false" here, is "state" in the localscript. (First argument)
        game.ReplicatedStorage.RemoteEvent:FireClient(game.Players[a.Parent.Name], false)
    end
end)

--localscript
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(state)
    game.Players.LocalPlayer.PlayerGui.ComputerGui.Battery.change.charging.Value = state
end)

--Structure
--[[

game
-Workspace
--Part
---Script

-ReplicatedStorage
--RemoteEvent

-StarterGui
--LocalScript
--ComputerGui

]]

--I haven't tested this code, so if anything errors, or something unexpected happens, feel free to comment on it.


0
Thank you so much! :) Now I know that Scripts cannot access the PlayerGui even if it isn't linked by LocalPlayer. hipenguinflip 30 — 7y
Ad

Answer this question