Datastore broken: no starting cash, money not saved + no data in console

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am making a sword fighting game, with a data store system where you start off with a set amount of cash (in this case 25), and you get 25 cash for every win. This game is based off of Alvinblox’s series (How To Make A Roblox Game - Saving Data (4) - YouTube), with some slight quality of life adjustments.

  2. What is the issue? Include screenshots / videos if possible!
    When joining the game with no previous data store, you don’t get the 25 cash (but it does give you the amount in Roblox Studio). Additionally, the cash doesn’t save, as to be seen in the video, where I gave myself 999 cash using the console. Lastly, when looking at the console, it appears that there is no data store at all.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have watched the video multiple times and checked every single line for mistakes, but to no avail. The script analysis and output show no errors either.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local dataStores = game:GetService("DataStoreService"):GetDataStore("CashDataStore")
local defaultCash = 25
local playersLeft = 0



game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats
	
	
	
	player.CharacterAdded:Connect(function(character)
		
		
		
		character.Humanoid.WalkSpeed = 16
		character.Humanoid.Died:Connect(function()
			-- Whenever someone dies, this event will run
			if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
				game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).." killed "..player.Name
				wait (1)
			end
			if character:	FindFirstChild("GameTag") then 
				character.GameTag:Destroy()
				end
				player:LoadCharacter()
			
		end)
		
	end)
	
	-- Data stores
	
	local player_data 
		
		pcall(function()
			player_data = dataStores:GetAsync(player.UserId.."-Cash")
		end) 
	
			
			if player_data ~= nil then 
			-- Player has saved data, load it in.
			cash.Value = player_data
		else 
			-- New player, give fixed amount of starting cash.
			cash.Value = defaultCash
		end
		
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		dataStores:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value)
		print("Data saved.")
		playersLeft = playersLeft - 1
		bindableEvent:Fire()
	end)	
end)

game:BindToClose(function()
	-- This will be triggered upon shutdown.
	while playersLeft > 0 do 
		bindableEvent.Event:Wait()
	end
end)

Thanks in advance for your help.

2 Likes

Error fixed: I forgot to enable HTTP requests and API services.

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