Why is my player Gui script not working?

Im trying to make a pad for the player where the player will have an activation Gui pop up so they can see the shop. Why is this erroring?

Script:

script.Parent.Touched:Connect(function()
	game.Players.LocalPlayer.PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = true
end)

script.Parent.TouchEnded:Connect(function()
	game.Players.LocalPlayer.PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = false
end)

Error Message:

  08:21:43.584  Workspace.ActivationPad.Pad.Pad:2: attempt to index nil with 'PlayerGui'  -  Server - Pad:2
  08:21:43.585  Stack Begin  -  Studio
  08:21:43.585  Script 'Workspace.ActivationPad.Pad.Pad', Line 2  -  Studio - Pad:2
  08:21:43.585  Stack End  -  Studio

Is your script a server script? You are using game.Players.LocalPlayer, which only works in local scripts.

3 Likes

Script must be like this probably >

script.Parent.Touched:Connect(function(BasePart)
if BasePart.Parent:FindFirstChild("Humanoid") then
game.Players:GetPlayerFromCharacter(BasePart.Parent).PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = true
end
end)

script.Parent.TouchEnded:Connect(function(BasePart)
if BasePart.Parent:FindFirstChild("Humanoid") then
game.Players:GetPlayerFromCharacter(BasePart.Parent).PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = false
end
end)
-- Try it.
1 Like

Can you explain how this works?

And also, nothing seems to be happening.

I was using a server script. But its detected by if the player touched a part.

Well, the script you sent won’t work in a server script. Doing game.Players.LocalPlayer on the server will try finding a player called “LocalPlayer”, which isn’t what you want to do.
Try the code that DeadBlox sent, instead of looking for the local player (which doesn’t work on the server), the code checks what player touched the part using GetPlayerFromCharacter and changes their gui.

When accessing the PlayerGui you must do this from a LocalScript. You can access the PlayerGui object itself, but not the contents itself.

You can easily switch your code to a LocalScript and keep using LocalPlayer.

You will need to check if what touched the part, is indeed the player and not another player or other object in your game.

If player will touch, the gui will be visible
else if player will stop touching then it will be invisible

this should do it :slight_smile:

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild('Humanoid') then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = true
	end
end)

script.Parent.TouchEnded:Connect(function(part)
	if part.Parent:FindFirstChild('Humanoid') then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.PlayerGui:WaitForChild("ToggleButtons").StanderedShopOpen.OpenShop.Visible = false
	end
end)