Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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)
0
Can you send the DataStore script? MegaManSam1 207 — 5y
0
Added the script now deadpanbotty 0 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
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

Ad
Log in to vote
-1
Answered by 5 years ago

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)
0
How can i use the value that comes from the datastore script then? deadpanbotty 0 — 5y
0
i-cant-data store. lol natonator63 29 — 5y
0
no a server script can see if a player is added and u dont make a boolvalue or intvalue Gameplayer365247v2 1055 — 5y
0
Anyway i can keep both the values the same then? deadpanbotty 0 — 5y

Answer this question