Why don't Leaderstats wont work?

hello I have problems with my code. I use the Leaderstats code and it doesn’t work

local DataStoreService = game:GetService("DataStoreService")
local store = DataStoreService:GetDataStore("StatsStore")
local Players =game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local data= store:GetAsync(Player.UserId)
	local stats = Instance.new('Folder')
	stats.Name = 'leaderstats'
	stats.Parent = Player
	local cash = Instance.new('IntValue')
	cash.Name = 'Cash'
	cash.Parent = stats
	cash.Value = data['Cash'] or 10
	local candy = Instance.new('IntValue')
	candy.Name = 'Candy'
	candy.Value = data['CandyA']
	candy.Parent = stats
	local multiplier = Instance.new('IntValue')
	multiplier.Name = 'Multiplier'
	multiplier.Value = 1
	multiplier.Parent = Player
	local speed = Instance.new('IntValue')
	speed.Name = 'Speed'
	speed.Value = 1
	speed.Parent = Player
	
	local function change()
		local t = {
		['Cash'] = Player:WaitForChild('leaderstats'):WaitForChild("Cash").Value,
		['CandyA'] = Player:WaitForChild('leaderstats'):WaitForChild("Candy").Value
	}
	store:SetAsync(Player.UserId,t)
	end
	
	candy.Changed:Connect(change)
	cash.Changed:Connect(change)
end)

Players.PlayerRemoving:Connect(function(Player)
	print('PlayerRemoving: ' .. Player.Name)
	local t = {
		['Cash'] = Player:WaitForChild('leaderstats'):WaitForChild("Cash").Value,
		['CandyA'] = Player:WaitForChild('leaderstats'):WaitForChild("Candy").Value
	}
	print(t)
	store:SetAsync(Player.UserId,t)
end

Why is the code not saved but takes the values?
what is she doing wrong with my code

(sorry if the grammar is bad, I use google translate)strong text

I am coding from the alvinbricks tutorial on the leading datastores

1 Like

my spanish language sorry if the grammar doesn’t suck

please help me with my code, she is playing with it, what is she doing?

Do the stats show up?
(30 Chars)

First where are you getting the "data[“CandyA”] from?

Edit: I advise you provide full codeif you havent

yes but no save

(30 characters)

this is the complete code

(30 characters)

in the save code I make a dictionary here

Players.PlayerRemoving:Connect(function(Player)
	print('PlayerRemoving: ' .. Player.Name)
	local t = {
		['Cash'] = Player:WaitForChild('leaderstats'):WaitForChild("Cash").Value,
		['CandyA'] = Player:WaitForChild('leaderstats'):WaitForChild("Candy").Value
	}
	print(t)
	store:SetAsync(Player.UserId,t)
end

I suggest making the Values 0 when you create then update them.
Edit: The Values you are getting from the datastore.

Instead of having a dictionary, it would probably be much easier just to make multiple datastores.

it’s supposed to be saved when i leave but it doesn’t work

its because the data doesn’t save making separate datastores is bad practice if you want to avoid this then simply use datastore2

Ok, Are you testing this in Studio?

but i heard it will queue it up and be not efficient

1 Like

yes, api services are yes

(30 character)

i do not make seperate data store

If you are Testing this in Studio, you may not have granted the Studio permissions to access Datastores.

simply use an easier datastore system like datastore2 or something else

if you want to avoid tons of datastores separate at each other

i did
(30 character)