Hello! So I have a leaderstats script and the only value currently is Tokens. I was wondering how can I add another Value?
local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = leaderstats
local UserId = player.UserId
local data
local succes, errormessage = pcall(function()
data = CoinStore:GetAsync(UserId)
end)
if succes then
Tokens.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local data = player.leaderstats.Tokens.Value
CoinStore:SetAsync(UserId, data)
end)
Just use Instance.new(), I don’t get your question though.
I request you to read it without ignoring.
Like this-
local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = leaderstats
--example--
local example = Instance.new(classname, leaderstats) --Basically second parameter sets the parent.
example.Name = "Example"
example.Value = whatever
local UserId = player.UserId
local data
local succes, errormessage = pcall(function()
data = CoinStore:GetAsync(UserId)
end)
if succes then
Tokens.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local data2 = player.leaderstats.Tokens.Value --as you have already assigned a global variable "data", so it would set your old data, so change the name of the variable to "Data2". Learn scopes to understand what I mean.
CoinStore:SetAsync(UserId, data2)
end)
You can even use a table to save multiple data, and there is one more trick besides table
local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = leaderstats
--example--
local example = Instance.new(classname, leaderstats) --Basically second parameter sets the parent.
example.Name = "Example"
example.Value = whatever
local UserId = player.UserId
local data
local succes, errormessage = pcall(function()
data = CoinStore:GetAsync(UserId)
end)
if succes then
Tokens.Value = data["Token"] or 0
Example.Value = data["Exp"] or 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data2 = {
["Token"] = player.leaderstats.Tokens.Value;
["Example"] = player.leaderstats.Example.Value;
}
local UserId = player.UserId
local success, err = pcall(function()
CoinStore:SetAsync(UserId, data2)
end)
if success then
print("Data Successfully Saved")
else
warn(err)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
local Default_Data = { -- create new table and put any stat that you want to make here ( gems , coins .etc
Tokens = 0;
Gems = 0;
}
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = leaderstats
local Gems = Instance.new("IntValue")
Gems .Name = "Gems"
Gems .Parent = leaderstats
local UserId = player.UserId
local data
local succes, errormessage = pcall(function()
data = CoinStore:GetAsync(UserId)
end)
if succes then -- once the data loaded successfully we wanna check if the player have data or not
-- if the player already have data we give him he's data from the data store else we give him the default one
data = data or Default_Data
Tokens.Value = data.Tokens
Gems .Value = data.Gems
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
-- and here should save the data with same way we getting it in Player add event function
local data = {
Tokens = player.leaderstats.Tokens.Value;
Gems = player.leaderstats.Gems .Value;
}
CoinStore:SetAsync(UserId, data)
end)
Hey, if you have tested my script, did it errored, if yes, then please show your code, if it works, please mark me as a solution so that many people could get it.
idk why i test it in studio and i work good but you can try this instead which fix the player add event may not trigger when test with 1 Player
local DataStoreService = game:GetService("DataStoreService")
local CoinStore = DataStoreService:GetDataStore("CoinStore")
local Default_Data = { -- create new table and put any stat that you want to make here ( gems , coins .etc
Tokens = 0;
Gems = 0;
}
local function OnPlayerAdd(Player: Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Tokens = Instance.new("IntValue")
Tokens.Name = "Tokens"
Tokens.Parent = leaderstats
local Gems = Instance.new("IntValue")
Gems .Name = "Gems"
Gems .Parent = leaderstats
local UserId = Player.UserId
local succes, data = pcall(function()
return CoinStore:GetAsync(UserId)
end)
if succes then -- once the data loaded successfully we wanna check if the player have data or not
-- if the player already have data we give him he's data from the data store else we give him the default one
data = data or Default_Data
Tokens.Value = data.Tokens
Gems.Value = data.Gems
end
end
game.Players.PlayerAdded:Connect(OnPlayerAdd)
for _ , player in game.Players:GetPlayers() do
task.spawn(OnPlayerAdd , player)
end
game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
-- and here should save the data with same way we getting it in Player add event function
local data = {
Tokens = player.leaderstats.Tokens.Value;
Gems = player.leaderstats.Gems.Value;
}
pcall( function() CoinStore:SetAsync(UserId, data) end)
end)
Oh yes! Sorry, I had a lot of stuff going on in real life for a few hours. But uhm, I do have a question. Do you want me to try to use the top code or bottom code?