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

Trying to use a number value in my leaderstats please help?

Ok so i`ve got a DataStore script which has its own Stats and im wanting to add these to values into my leaderstats but ive got no clue what im doing.

I have tried to figure it out on my own but it aint working heres the script,

local Player = game.Players.LocalPlayer

game.Players.PlayerAdded:connect(function(plr)

local stats = Instance.new("BoolValue", plr)
stats.Name = "leaderstats"

local money = Instance.new("IntValue",stats)
money.Name = "Coins" 
money.Value = Player.PlayerStats.Coins.Value

end)

Data store script below

local PlayerStatsDS = game:GetService("DataStoreService"):GetDataStore("Test60")

game.Players.PlayerAdded:Connect(function(NP)

local Key = "PDS-".. NP.UserId

local GetSave = PlayerStatsDS:GetAsync(Key)

local PSF = Instance.new("Folder", NP)
PSF.Name = "PlayerStats"

local StatsFolder = script.Stats

for _, S in pairs(StatsFolder:GetChildren()) do
local NS = Instance.new(S.ClassName, PSF)
NS.Name = S.Name
NS.Value = S.Value
end

if GetSave then
for n, Stat in pairs(PSF:GetChildren()) do
warn("Loaded " .. Stat.Name .. " | " .. Stat.Value .. "!")
Stat.Value = GetSave[n]
end
else

local STS = {}

for _, Stat in pairs(StatsFolder:GetChildren()) do
table.insert(STS, Stat.Value)
warn("Saving " .. Stat.Name .. " | " .. Stat.Value .. "!")
end
PlayerStatsDS:SetAsync(Key, STS)
end
end)



game.Players.PlayerRemoving:connect(function(OP)

local Key = "PDS-".. OP.UserId

local StatsFolder = OP.PlayerStats

local STS = {}
for _, Stat in pairs(StatsFolder:GetChildren()) do
table.insert(STS, Stat.Value)
warn("Player Left,Saving " .. Stat.Name .. " | " .. Stat.Value .. "!")
end
PlayerStatsDS:SetAsync(Key, STS)
end)

2 comments

0

Can you send the DataStore script?

0

Added the script now

2 answers

local Datastore = game:GetService("DataStoreService"):GetDataStore("Speed")

  

game.Players.PlayerAdded:Connect(function(player)--when a player joins it connects the function below just as in ur own script

  

local Key = "Player-ID:" .. player.UserId--this ofcourse makes a key for every player

  

local leaderstats = Instance.new("Folder", player)--this creates the folder for the player

  

leaderstats.Name = "leaderstats"--and here we name it, name it whatever u want

  

local what u want to save here = Instance.new("NumberValue", leaderstats)--creates a number value inside leaderstats

  

same name here.Name = "Name here"--just names the leaderstat

  

game:GetService("ReplicatedStorage"):WaitForChild("Name here").OnServerEvent:Connect(function()--when the server event fires then it connects the function below

  

ur leaderstat name.Value = ur leaderstat name.Value + value of choice, must be a number

end)


  

-- GetAsync

  

local GetSave = Datastore:GetAsync(Key)--gets the key for the player

  

if GetSave then--if it can save

  

leaderstat name.Value = GetSave[1]--then it saves the value


  

else

  

local Numbers = {leaderstat name.Value}--puts in the values into the local Numbers

  

Datastore:SetAsync(Key, Numbers)--saves the Numbers into the key

  

  

end

  

end)

  

game.Players.PlayerRemoving:Connect(function(player)--when the player leaves

  

local Key = "Player-ID:" .. player.UserId

  

local ValuesToSave = {player.leaderstats.leaderstat name.Value}--u understand from what it says

  

Datastore:SetAsync(Key, ValuesToSave)


end)

here is my working leaderboard script

First of all, you are in a server script, so you cant use game.Players.LocalPlayer and second, you tried identify playerstats when you created something with the name "leaderstats" here is the fix:

game.Players.PlayerAdded:connect(function(plr) -- put me in serverscriptservice and make me a normal script

  

local stats = Instance.new("BoolValue", plr)

stats.Name = "leaderstats"

  

local money = Instance.new("IntValue",stats)

money.Name = "Coins"

money.Value = plr.leaderstats.Coins.Value

  

end)

4 comments

0

How can i use the value that comes from the datastore script then?

0

i-cant-data store. lol

0

no a server script can see if a player is added and u dont make a boolvalue or intvalue

0

Anyway i can keep both the values the same then?

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