Data wont save for some reason

local Players = game:GetService("Players")


local startamount = "N/A" 

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Data")


game.Players.PlayerAdded:connect(function(player)
	local Folder = Instance.new("Folder",player)
	Folder.Name = "Data"
	
	
	
	local RaceData = Instance.new("StringValue",Folder)
	RaceData.Name = "Race"
	local CoatValue = Instance.new("StringValue",Folder)
	CoatValue.Name = "Coat"

	RaceData.Value = ds:GetAsync(player.UserId) or startamount
	CoatValue.Value = ds:GetAsync(player.UserId) or startamount

	ds:SetAsync(player.UserId, RaceData.Value, CoatValue.Value)
	
	
	
	RaceData.Changed:connect(function()
		ds:SetAsync(player.UserId, RaceData.Value)
	end)
	
	CoatValue.Changed:connect(function()
		ds:SetAsync(player.UserId, CoatValue.Value)
	end)
	
	
end)

I have this script here which sets the data but it wont ever save whenever it changes, or when I leave, any help?

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId.." Data1", player.Data.Race.Value) 
	ds:SetAsync(player.UserId.." Data`", player.Data.Coat.Value) 
end)

This is the script of me setting the data

	if player.Data.Race.Value == "N/A" then

		
		local math = math.random(1,3)
		if math == 1 then
			player.Data.Race.Value = "FlameSoul"
			
	
			
		elseif math == 2 then
			player.Data.Race.Value = "FrigidWalker"
			
		elseif math == 3 then
			player.Data.Race.Value = "SkySeeker"
			
		end
	end

Yes API services is on.

1 Like

You can do a protected call to show you what the error is (and to protect the datasaving incase anything happens).

Example:

RaceData.Changed:connect(function()
		ds:SetAsync(player.UserId, RaceData.Value)
	end)
	
	CoatValue.Changed:connect(function()
              local success, errormsg = pcall(function()
                 ds:SetAsync(player.UserId, CoatValue.Value)
        end
end)

if success then
    --your code
else
     warn(errormsg)
end

And same thing for your racedata.

(hope this helps)

1 Like

You aren’t using the same keys when saving:

RaceData.Value = ds:GetAsync(player.UserId) or startamount
CoatValue.Value = ds:GetAsync(player.UserId) or startamount

should be

RaceData.Value = ds:GetAsync(player.UserId.." Data1") or startamount
CoatValue.Value = ds:GetAsync(player.UserId.." Data`") or startamount
1 Like