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

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

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)

1 comment

0

replace line 5 with "plr.PlayerGui.Money.TextLabel.Number.Value -= 50"

2 answers

✓ Accepted answer

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.

1 comment

0

Thank you so much!

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.

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