Datastore recieving false values no matter what

i am currently making a permadeath kind of game, and im implementing an “in danger” feature to prevent people from leaving the game and recieving no punishment, but for some reason even if the “InDanger” value is true it will return in false no matter what i do, anyone has an idea on why this happens, and could give me a fix?
Edit: The datastore itself has started breaking and no longer wants to save values like “speed”
Edit #2: i managed to find a way to fix the other values refusing to save, i havent been able to fix the “InCombat” Value yet

Code
local datastore = game:GetService("DataStoreService"):GetDataStore("DusunV2#0.1")
game.Players.PlayerAdded:Connect(function(player)
	--<<Folder>>	
	local StatsFolder = Instance.new("Folder")
	StatsFolder.Name = "Data"
	StatsFolder.Parent = player
	warn("Folder has been set up")
	--<<Values>>
	local Charisma = Instance.new("IntValue")
	Charisma.Name = "Charisma"
	Charisma.Parent = StatsFolder

	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength"
	Strength.Parent = StatsFolder

	local Speed = Instance.new("IntValue")
	Speed.Name = "Speed"
	Speed.Parent = StatsFolder


	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Parent = StatsFolder

	local InCombat = Instance.new("BoolValue")
	InCombat.Name = "InCombat"
	InCombat.Parent = StatsFolder
	
	warn("Values have been set up")
	
	local key = "user-" .. player.userId
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		Strength.Value = storeditems[1] 
		Speed.Value = storeditems[2]
		InCombat.Value = storeditems[3]
		warn(player.Name.."'s data has loaded... ID"..player.userId)
		wait(1)
		if InCombat.Value == true then
			warn(player.Name.." HAS BEEN FOUND IN COMBAT... ID"..player.UserId)
			InCombat.Value = false
		end		
	else
	local items = {Strength.Value, Speed.Value, InCombat.Value} 
		datastore:SetAsync(key, items)
		warn(player.Name.." has no data... ID"..player.UserId)

		end
	
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.Data.Strength.Value, player.Data.Speed.Value}
	local key = "user-" .. player.userId
	warn(player.Name.."'s data has been saved... ID"..player.UserId)

	datastore:SetAsync(key, items)
end)

First of all, I should point out that I don’t see you saving the InCombat value in PlayerRemoving Event, only the Speed and Strength value.

Some more things to note, please use pcalls so that incase of errors, you know what to do, like retries if you want etc. Also :connect is deprecated now I believe, use :Connect instead. Also use BindToClose so that data loss doesn’t occur for players when Server shutdowns or something happens.

4 Likes

You can watch my tutorial that explains Basic Datastore, and just as @WaterJamesPlough said you didn’t save the InCombat value,

Heres my tutorial on datastore: Datastore Tutorial for Beginners

1 Like

the :Connect part was the one causing the problem, thanks alot for your help

1 Like

I still am afraid that it will error, because look at it once. You will over write the data table on player leave and the InCombat value will be nil again, so you might want to look through it once more.

Its working fine for me, ill try using more efficient ways to save data when the player leaves though

i have encountered another problem, for some reason the “InCombat” value is returning to true even though its false, any solutions?