Why doesn't my PlayerGui script work?

  1. Basically, when a player presses a gui button, every player in the server gets 2 images appeared in their player gui’s.

  2. When I press the button, everything’s working perfectly EXCEPT nobody gets the images popped up, just the Player who pressed the button. This is the error code:
    line 13, attempting to index nil with ‘CharacterStuff’

  3. This is the script:
    local button = script.Parent
    local Player = game.Players.LocalPlayer
    local Clone = game.ReplicatedStorage.Characters.Koto.Koto

button.MouseButton1Click:Connect(function()
if script.Parent.Parent.HasCharacter.Value == false then
script.Parent.Parent.HasCharacter.Value = true
print(‘Koto Chosen!’)
for _, player in ipairs(game:GetService(“Players”):GetPlayers()) do
local playerGui = player:FindFirstChild(“PlayerGui”)
Player.Character = Clone
Player.Character.Parent = game.Workspace
playerGui.CharacterStuff.CharacterSelection.A5.Visible = false
playerGui.CharacterStuff.CharacterSelection.A5Silouhette.Visible = true
playerGui.CharacterStuff.CharacterSelection.A5Blood.Visible = true
script.Parent.Parent.Parent.Enabled = false
end
end
end)

I Tried doing playerGui:WaitForChild(“CharacterStuff”) but still same error
Also its a local script inside the button
Im stuck :sob:

2 Likes

You can’t edit other players GUI on a client script, you’ll have to use a remote event to communicate with the server and have the server do it.

1 Like

As tay_z3r already mentioned, accessing the playerGui of another client from a localscript is not possible.

Create a RemoteEvent inside ReplicatedStorage and call it whatever you want.
In your localscript you will simply put:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("MyEvent")

-- your main code here

-- now instead of executing the ipairs loop and already applying changes, simply say:
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
    event:FireServer(player)
end

Also be sure that you don’t have a conflict between your define variable “player” and “Player”.

Now, you just fire off a remote event. You will now need to make a server sided script listening to that event.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("MyEvent")

event.OnServerEvent:Connect(function(plr, target)
    -- target is the "player" in the localscript shown above
    -- here you can now access playerGui and apply the actual changes
end)

Here is a link to Roblox’s offical document regarding Remote Events and how to handle them properly.

Hope this helped!

1 Like

Tysm for helping me understand!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.