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

How do you fix this leaderboards script for my game?

Okay, so basically I tried making a leaderboard script and I cannot fix it at the moment. Its not an localscript its a regular script and I dont understand how its not working. Its 100% broken right now.

local DataStore = game:GetService("DataStoreService")
local CashStore = DataStore:GetDataStore("CashStore")
local RebirthStore = DataStore:GetDataStore("RebirthStore")
local StageStore = DataStore:GetDataStore("StageStore")

game.Players.PlayerAdded:Connect(function(player)
 
 local stats = Instance.new("Folder",player)
 stats.Name = "leaderstats"
 
 local cash = Instance.new("IntValue",stats)
 cash.Name = "Cash"
 cash.Value = CashStore:GetAsync(player.UserId) or 0
 print("Data Loaded.")


local rebirths = Instance.new("IntValue",stats)
rebirths.Name = "Rebirths"
rebirths.Value = RebirthStore:GetAsync(player.UserId) or 0
print("Data Loaded")

local stage = Instance.new("IntValue",stats)
stage.Name = "Stage"
stage.Value = StageStore:GetAsync(player.UserId) or 0
print("Data Loaded")


 
end)

game.Players.PlayerRemoving:Connect(function(player)
 CashStore:SetAsync(player.UserId,player.leaderstats.Cash.Value)
 print("Data Saved.")

end)?

game.Players.PlayerRemoving:Connect(function(player)
    RebirthStore:SetAsync(player.UserId,player.leaderstats.Rebirths.Value)
end)


game.Players.PlayerRemoving:Connect(function(player)
    StageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
end)

2 comments

0

Where the hell did all those end)’s come from?!

0

Also don’t use AlvinBlox, or free Scripts, they’re very deprecated

1 answer

✓ Accepted answer

Ok, so firstly, since these are all IntValues, I see no reason why you can't use a Table to Save the Values, rather than three different DataStores.

Secondly, sometimes when using the GetAsync - or method, the getasync is read as nil and kept as the value of the item, rather than your "or" value, so you should preset the intvalue and check if there is data that needs to be loaded, since if the player has never played the game, then they have no DataStore info (the getasync would return nil)

local DataStore = game:GetService("DataStoreService")
local Storage = DataStore:GetDataStore("Numerical")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"

	local cash = Instance.new("IntValue", stats)
	cash.Name = "Cash"
	cash.Value = 0

	local rebirths = Instance.new("IntValue", stats)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	local stage = Instance.new("IntValue", stats)
	stage.Name = "Stage"
	stage.Value = 0

	local LoadArray = Storage:GetAsync(player.UserId)
	if LoadArray then
		player.leaderstats.Cash.Value = LoadArray[1]
		player.leaderstats.Rebirths.Value = LoadArray[2]
		player.leaderstats.Stage.Value = LoadArray[3]
		print("Data Loaded.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local SaveArray = {player.leaderstats.Cash.Value, player.leaderstats.Rebirths.Value, player.leaderstats.Stage.Value}
	Storage:SetAsync(player.UserId, SaveArray)
	print("Data Saved.")
end)

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