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

GUI giver OnClick working perfectly in studio, but not in game server?

So my GUI is working perfectly fine in studio, but not in game. the Server log Error in game reads:

Workspace.Part.Script:5: attempt to index field "LocalPlayer" (a nil value)

Here is my script

local Gui = script.Parent.CardGiverGui

function onClicked()
    local GUI = Gui:Clone()
    GUI.Parent = game.Players.LocalPlayer.PlayerGui
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

✓ Accepted answer

The problem is that only a local script has access to the property LocalPlayer.

The MouseClick event passes the player who clicked it as an arg.

Example(Server Script):-

local Gui = script.Parent.CardGiverGui

function onClicked(plrWhoClicked)
	-- you might also want to check if they have the gui
   Gui:Clone().Parent = plrWhoClicked.PlayerGui
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I hope this help.

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