Trying to figure out why my other stats wont show when I play the game on the leaderboard. Please reply and let me know what errors I have.
Current Code:
local DataStoreService = game:GetService("DataStoreService")
local Hops_DSS = DataStoreService:GetDataStore(“Coins_Data_Store”)
local Coins_DSS = DataStoreService:GetDataStore(“Notes_Data_Store”)
local Diamonds_DSS = DataStoreService:GetDataStore(“Diamonds_Data_Store”)
game.Players.PlayerAdded:Connect(function(player)
local Key = “user_”…player.UserId
local Stats = Instance.new("IntValue",player)
Stats.Name = "leaderstats"
local Hops = Instance.new("IntValue",Stats) --Note to self: Changed name of stats for stealing purposes
Hops.Name = "Hops"
Hops.Value = Hops_DSS:GetAsync(Key) or 0
local Coins = Instance.new("IntValue",Stats)
Coins.Name = "Coins"
Coins.Value = Coins_DSS:GetAsync(Key) or 0
local Diamonds = Instance.new("IntValue",Stats)
Diamonds.Name = "Diamonds"
Diamonds.Value = Diamonds_DSS:GetAsync(Key) or 0
Hops.Changed:Connect(function()
Hops_DSS:SetAsync(Key, Hops.Value)
end)
Coins.ChildAdded:Connect(function()
Coins_DSS:SetAsync(Key, Coins.Value)
end)
Diamonds.Changed:Connect(function()
Diamonds_DSS:SetAsync(Key, Diamonds.Value)
end)
i do believe that saving stats each time they change isn’t very practical.
saving them upon the user leaving and binding the game when closed is alot better.
anyway i’ve read the code and it looks to me that it stops after the stat hops is instanced and i believe something is “erroring” before or when hops is instanced.
Maybe print some stuff out and check what prints and what doesn’t.
Using .Changed will practically make you reach the limit of saving the data which means the player’s data wont be saved after the limit has been reached, as Snoopy1333 has mentioned, you should just save upon player removing to avoid reaching the limit.
Also I have just noticed, you are creating an IntValue instead of a folder, that may be your problem.
Since you posted no output and your DataStore requests aren’t wrapped in pcalls it’s hard to isolate where the issue is coming from. Please provide your output and that will help us out. And please do check in game settings if studio has access to API requests.
Your datastoring is bad practice, please use my example as to a better datastore.
--// Services \\--
local Players = game:GetService("Players");
local DataStoreService = game:GetService("DataStoreService");
--// Datastores \\--
local MainData = DataStoreService:GetDataStore("Main");
--// Tables \\--
local PlayerData = {};
--// Events \\--
Players.PlayerAdded:connect(function(player)
local UserData= MainData:GetAsync(player.UserId);
--// Would personally use a function to check this, or better a module.
if not UserData then
UserData= {
Hops = 0,
Coins = 0,
Diamonds = 0,
}
end
--// If you use multiple scripts I recommend making a module that will return any data point from the "PlayerData" table
PlayerData[player.UserId] = UserData; --// What you'll use to change the values
local Stats = Instance.new("Folder",player);
Stats.Name = "leaderstats";
local Hops = Instance.new("IntValue",Stats) ;
Hops.Name = "Hops";
local Coins = Instance.new("IntValue",Stats);
Coins.Name = "Coins";
local Diamonds = Instance.new("IntValue",Stats);
Diamonds.Name = "Diamonds";
end)
Players.PlayerRemoving:connect(function(player)
local UserData = PlayerData[player.UserId];
if UserData then
MainData:SetAsync(player.UserId, UserData);
PlayerData[player.UserId] = nil;
else
warn("Did not find any data");
end
end)