What The Problem Is:
So when changing leader-stat values I only know how to script it for the client, but that does not change the values correctly so how would I be able to do it using the server?
How It Doesn’t Work:
When using the client for changing values it doesn’t change it fully as in if you had a currency increase script and you changed the value, when your currency increased it would increase from the amount you had before you changed the value.
Finding Answers:
I have tried looking it up on the web, but I couldn’t find any solutions, I also tried to Fire a remote event, but how would you put this together if you could? (Not asking for a full script just an example).
I hope I do find an answer to this because it is very crucial to my game, please tell me anything you know on this, thanks.
1 Like
– An example server script, put inside of a players character by putting it in StarterCharacterScripts
local playerModel = script.Parent
local player = game.Players:GetPlayerFromCharacter(playerModel)
player:WaitForChild(‘leaderstats’)
player.leaderstats:WaitForChild(‘money’) – This would be whatever your leaderboard value is
local cash = player.leaderstats.money
while true do
wait(5)
cash.Value = cash.Value + 100
end
Hope my example helps in some way!
9 Likes
thanks I will look into this.![:grin: :grin:](https://doy2mn9upadnk.cloudfront.net/images/emoji/twitter/grin.png?v=12)
Don’t forget the .Value after the cash, i just noticed it was missing from mine and added it late lol.
yes definitely that would be embarrassing
A complete server-sided script without duplicates, also eliminates value objects:
local Players = game:GetService("Players")
local playerStats = {}
local function getDefaultStats()
return {
Cash = 0;
Experience = 0;
}
end
Players.PlayerAdded:Connect(function(player)
playerStats[player.UserId] = getDefaultStats()
-- optional
while player.Parent do
wait(5)
if playerStats[player.UserId] then -- oops
playerStats[player.UserId]["Cash"] = playerStats[player.UserId]["Cash"] + 100
end
end
end)
Players.PlayerRemoving:Connect(function(player)
playerStats[player.UserId] = nil
end)
In order to change it from server, you will have to find the player’s UserId and change the values in the scope of the script.
That may run too but it just looks a little more complicated lol. I was just giving him a simple example of how to change stats for one player since he seems new to server side leaderboard scripting. Bacon if you want to change the stats off all players you could just do this too with one server script in the workspace:
while true do
wait(5)
local kids = game.Players:GetChildren()
for i=1, #kids do
if kids[i]:FindFirstChild('leaderstats')~=nil and kids[i].leaderstats:FindFirstChild('money')~=nil then
kids[i].leaderstats.money.Value = kids[i].leaderstats.money.Value + 100
end
end
end
1 Like
thanks guys it worked really appreciate it.![:grin: :grin:](https://doy2mn9upadnk.cloudfront.net/images/emoji/twitter/grin.png?v=12)
Happy Developing!
I also edited @MarineMike826 script so it only does it once and his script was easier to understand I appreciate it.
I plan on adding the FireServer function to this as well please note.
Example For New Viewers
local playerModel = script.Parent
local player = game.Players:GetPlayerFromCharacter(playerModel)
player:WaitForChild("leaderstats")
player.leaderstats:WaitForChild("Cash")
local cash = player.leaderstats.Cash
if cash.Value>=10 then
cash.Value = cash.Value - 10
end
2 Likes
Just so you know, you don’t have to have the while do loop in there. I only did that incase you were going to be giving the player money every so often like a paycheck. But if you just want to take away cash like as in the player is purchasing something, you could just do if player.leaderstats.cash.Value>=10 then player.leaderstats.cash.Value = player.leaderstats.cash.Value - 10 end
yeah if I did it the loop way it would just be a big mess thanks