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

How to Access a player's GUI via a normal script?

Asked by 4 years ago

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)

0
replace line 5 with "plr.PlayerGui.Money.TextLabel.Number.Value -= 50" User#30567 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

0
Thank you so much! wolftamer894 50 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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.

Answer this question