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

Leaderstats mouseclick function not working?

This script is in game.Workspace




game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	
	
	
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "Cash"
	
end)

This script is in game.StarterGui.ScreenGui.TextButton.Script




local CashGivenButton = script.Parent






CashGivenButton.MouseButton1Up:Connect(function(player)
	
	local PlayerCash = player.leaderstats.Cash
	
	
	PlayerCash.Value = PlayerCash.Value + 1
	
	
end)

1 answer

✓ Accepted answer

There are problems with the placements and the script themselves. First of all, your leaderstats script should be in ServerScriptService, since it's gonna be replicated across all players who join the game. Secondly, your script should be a localscript, the main reason why is because the StarterGui will replicate to the Player's PlayerGui. Also put your localscript below the ScreenGui, not under the TextButton. Put a remote event in ReplicatedStorage and change the positions of your script.

The Leaderstats Script

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = leaderstats --Use this method, as the 2nd parameter of Instance.new() is actually slower.
	leaderstats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
end)

So about the remote event part I added. When you use FireServer(), the parameter called player is always sent to the OnServerEvent. You can use the player parameter to increase the cash. You'll see the FireServer() part on the localscript below.

The GUI Local Script

local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local cashGivenButton = script.Parent.CashGiveButton

cashGivenButton.MouseButton1Up:Connect(function()
	remoteEvent:FireServer() --We do not need any extra parameters.
end)

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