Instead of using one script use a remote event and send over the player information like this
Inside the button:
local player = game.Players.LocalPlayer
local Cash = player.leaderstats:FindFirstChild("Cash")
script.Parent.MouseButton1Click:Connect(function()
if Cash.Value > 0 then
game.ReplicatedStorage.RE:FireServer(player)
end
end)
If Click is an item inside a ScreenGui then clicked.Parent would not be the Character.
clicked.Parent would be the ScreenGui since the button is inside that folder.
So, local plr = game.Players:GetPlayerFromCharacter(char) would not work because you are using the ScreenGui folder to find the player you already have.
At least, that is what it looks like you are doing.
Well, taking in to account what @mc7oof said above, you would probably want to listen in ServerScriptService (I would heavily recommend implementing the system @stormmaster9090 recommended because itâs way better, but this would require minimal changes)
local function listen(player:Player)
local function addCash()
player.leaderstats.Coins.Value += 1
end
local button = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Click")
button.MouseButton1Click:Connect(addCash)
end
game:GetService("Players").PlayerAdded:Connect(listen)
This looks messy and isnât the best for efficiency, please use the method suggested by @stormmaster9090 .
You should use a RemoteEvents instead âŚ
Also server/legacy scripts wonât run on client focused services like StarterGui
Hereâs how your scripts should look:
For the localscript in StarterGui
local Button = script.Parent
Button.MouseButton1Click:Connect(function()
game:GetService('ReplicatedStorage').CoinEvent:FireServer()
end)
The only problem with that is itâs extremely easy for exploiters to give themself a ton of money really easily. Do not trust the client with anything like that. Instead, manage it all on the server.
Yes, I understand. I just wanted to keep it simple for the OP, since I could see that heâs a beginner scripter, and also due to this being in StarterGui exploiters do not have âan easierâ way of getting money, since itâs replicated for everyone. However a debounce should be the minimum in the server script.
I get what you mean about keeping it simple, but exploiters with script injectors, etc. can spam the event, and even with a debounce they can get a big advantage.
I donât need script injection to spam the event, itâs replicated for everyone, just use an autoclicker and it will result in almost the same thing as using external software, however neither will bypass a server-side debounce, but yeah if itâs not replicated for every client then exploiters will have an advantage.
Place the server Script inside of the button named Click
Replace the code with this:
local click = script.Parent
local player = click:FindFirstAncestorWhichIsA("Player")
click.MouseButton1Click:Connect(function()
player.leaderstats.Coins.Value += 1
end)
Do note that MouseButton1Click doesnât provide a parameter, so an alternative method to get the player needs to be used, like the one in my example