Hi, I am currently making a game that uses Data Store 2 to save the data. I made a script that works but only shows the leaderstats every second time I join in the game. For example, if I join the first time, it will show that that I have 100 Coins and 0 Gems but when I leave and join the game again, the whole leaderboard just disappears. When I leave and join for the third time, it suddenly reappears again. Please help.
Script:
local DataStore2 = require(1936396537)
DataStore2.Combine("Key","Coins","Gems")
game.Players.PlayerAdded:Connect(function(plr)
local dataCoins = DataStore2("Coins",plr)
local dataGems = DataStore2("Gems",plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
local gems = Instance.new("IntValue",leaderstats)
gems.Name = "Gems"
coins.Value = dataCoins:Get()
gems.Value = dataCoins:Get()
if dataCoins:Get() ~= nil then
coins.Value = dataCoins:Get()
else
coins.Value = 0
end
if dataGems:Get() ~= nil then
gems.Value = dataGems:Get()
else
gems.Value = 0
end
coins.Changed:Connect(function()
dataCoins:Set(coins.Value)
end)
gems.Changed:Connect(function()
dataGems:Set(gems.Value)
end)
end)
I have a part with a clickdetector on it which gives me 10 coins every time I press it.
script.Parent.ClickDetector.MouseClick:Connect(function(player)
print(player)
local Coins = player.leaderstats.Coins
Coins.Value = Coins.Value + 10
end)
