I am trying to make a button that, upon being clicked, will subtract GUI currency. However I can't say "game.Players.LocalPlayer.etc..." because "localPlayer" does not work in a normal script. Here is the code I have now:
script.Parent.MouseClick:Connect(function(plr) if game.Workspace.Data.Buildingname.Value == ("ArcherHut") then game.Workspace.ArcherHut:SetPrimaryPartCFrame(CFrame.new(-731.5, 6.5, -2676.5)) -- sets moedel cframe. wait(0.1) game.Players.LocalPlayer.PlayerGui.Money.TextLabel.Number.Value = game.Players.LocalPlayer.PlayerGui.Money.TextLabel.Number.Value - 50 end end)
try this.
script.Parent.MouseClick:Connect(function(plr) if game.Workspace.Data.Buildingname.Value == ("ArcherHut") then game.Workspace.ArcherHut:SetPrimaryPartCFrame(CFrame.new(-731.5, 6.5, -2676.5)) -- sets moedel cframe. wait(0.1) local money = plr.PlayerGui.Money money.TextLabel.Number.Value = money.TextLabel.Number.Value - 50 end end)
solution: as you already know, game.Players.LocalPlayer cant be called from server script, but only client scripts. When the player clicks whatever they click in this, the (function(plr) part gets the player who clicked it.
The server cannot access the descendants of a PlayerGui
through normal means of FilteringEnabled
. Any edits done to any GUI object in the PlayerGui
must be done from a local script.
Looking at your code, you can easily convert a ClickDetector
event to a GUIButton
event; you just need to specify what mouse button is being pressed. If necessary, you can use a RemoteEvent
to place the hut on the server so that everyone can see the placement.