Title. I want to make a tool that, when clicked, increases an exsisting leaderboard through a script (not local). I tried to start one on my own, but It didn't work without using a local script.
For some reason, when I use a LocalScript, the two leaderboards called don't match.
Leaderboard Script:
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Model") leaderstats.Name = "leaderstats" leaderstats.Parent = player local money = Instance.new("IntValue") --We create a new IntValue money.Name = "Cash" --this is the name you want the leader-stat to be when it shows up in-game. money.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later) money.Parent = leaderstats -- Set the money object as a child of the leaderstats object. end)
Tool Script:
game.Players.PlayerAdded:Connect(function(player) local tool = script.Parent tool.Activated:Connect(function() player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 end) end)
Thanks in advance!
I'm not understanding what the PlayerAdded
event is for in your tool script. A tool in the StarterPack
of a player will always be placed in the backpack when a player spawns. What this means for you is that the player will be the script.Parent.Parent.Parent (assuming that the script is a child of the tool and not the handle). Knowing this, the script could look like this:
local tool = script.Parent local player = script.Parent.Parent.Parent tool.Activated:Connect(function() player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 end)
EDIT: it is mandatory that this script is a server script
For first, your leaderstats script do not contains any errors. In your Tool script use a Server Script and put this code:
local player_name = script.Parent.Parent.Parent.Name -- Find player with ServerScript. local player = game:GetService("Players"):FindFirstChild(player_name) -- Find the player with name. local leaderstats = player:FindFirstChild("leaderstats") -- Find leaderstats local cash = leaderstats:FindFirstChild("Cash") -- Currency item. local cash_for_add = 1 -- Money for player earn local tool = script.Parent -- Tool location tool.Activated:Connect(function() -- On click with Mouse in tool if player and leaderstats and cash then cash.Value = cash.Value + cash_for_add -- Gives the money end end)
Hope it helped :)
Errors? tell-me on comments