My Data Store script wont work

So I made this script so it saves there credits or money when the leave the game and I put the default to ten credits and nothing will work.
Here is the script I have made:

local dataStores = game:GetService("DataStoreService"):GetDataStore("CreditsDataStore")
local defaultCash = 10
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local credits =  Instance.new("IntValue")
	credits.Name = "Credits"
	credits.Value = 0
	credits.Parent = leaderstats
	
	
	player.CharacterAdded:Connect(function(character)
		character.humanoid.WalkSpeed = 16		
		character.humanoid.Died:Connect(function()
			--They die The script Runs
			if character:FindFirstChild("GameTag")then
				character.GameTag:Destroy()
			end
			
			player:LoadCharacter()
		end)
		
	end)
	
	--Data Store Stuff EPIC WAS HERE
	
	local player_data
	
	pcall (function()
		player_data = dataStores:GetAsync(player.UserId.."-Credits")
	end)
	
	if player_data ~= nil then 
		--player has save data so it will load in
		credits.Value = player_data
	else
		-- New Player Of the Game
		credits.Value = defaultCash
	end
	
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	
	pcall(function()
		dataStores:SetAsync(player.UserId.."-Credits",player.leaderstats.Credits.Value)
		print("Data Has Been Saved No errors")
		playersLeft = playersLeft - 1
		bindableEvent:Fire()
	end)
	
end)

game:BindToClose(function()
	--Will Start when shut down happens
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

Can you send if you have any errors?

Can you be more specific with what does not work? I did notice something though. You have chracter.humanoid.Walkspeed and character.humanoid.Died. Those typically should say character.Humanoid with an uppercase “h.” That is, unless you changed the name. Also, Instead of using a variable to track the number of players, you can use #game.Players:GetPlayers().

I have it to where the default cash is 10 and when ever a new player loads in they should get 10 credits and when i try to run the script in the game it stays at 0

Yeah, i see. You need to change the credits.Value = 0 to credits.Value = 10.

I have tried that and got no where with it

Reset your datastore, maybe your original value which is 0 has already been saved.

1 Like

And how do I reset the data Store?

Simply use a new one, change the name.

3 Likes

I feel very unworthy of coding now I didnt think of that