Leaderstats acting strange

Hello.
I literally have no idea why this isn’t working. Here is my code:

local players = game:GetService("Players")
local DSService = game:GetService("DataStoreService")
local database = DSService:GetDataStore("TestDatabase")

local function playerAdded(player)
	local leaderstats = Instance.new("Configuration", player)
	leaderstats.Name = "leaderstats"

	local Cash = Instance.new("NumberValue", leaderstats)
	Cash.Value = database:GetAsync("Cash-"..player.userId) or 0
	Cash.Name = "Cash"

	local Relocation = Instance.new("NumberValue", leaderstats)
	Relocation.Value = database:GetAsync("Relocation-"..player.userId) or 0
	Relocation.Name = "Relocation"
end

local function playerRemoving(player)
	database:SetAsync("Cash-"..player.userId, player.leaderstats.Cash.Value)
	database:SetAsync("Relocation-"..player.userId, player.leaderstats.Relocation.Value)
end

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerRemoving)

Here is a studio screenshot to show I’m not going insane:
what

Hopefully one of you can point me in the right direction.

  • Ham

@FoxysCove102 Not quite sure while this is happening, but I do notice some other things:

  1. Don’t use the parent parameter in the Instance.new() function, this can cause performance issues. Set the parent after you set all the properties so it only has to replicate once. (More on why not to use: PSA: Don't use Instance.new() with parent argument ). This could have something to do with it but I doubt it

  2. Always use a pcall when dealing with datastores in case of failure

Nevermind. It just randomly started working…gotta love roblox.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.