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

Using Touched and TouchEnded to change value in GUI?

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

2 comments

0

Is this a localscript? Picture of your startergui?

0

This is a script in a part in workspace.

1 answer

✓ Accepted answer

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.

	

1 comment

0

Thank you so much! :) Now I know that Scripts cannot access the PlayerGui even if it isn't linked by LocalPlayer.

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