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

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

Asked by 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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.

Ad

Answer this question