DataStore kicks players who's (Values) are either nil or 0

I have a simple datastore script that works fine but however it only works if player value is set to more than 0(or1) is there any ways to prevent this?
I figured this out by joining on my alt who never joined before.
Edit: Rejoining seems to fix this but why does it kick in the first place?

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")
local __ISVIP = game:GetService("ReplicatedStorage"):FindFirstChild("__ISVIP")
--local __LOADED = game:GetService("ReplicatedStorage"):FindFirstChild("__VALUES"):FindFirstChild("__LOADED")
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
	
    local TIX = Instance.new("IntValue")
    TIX.Name = "TIX"
    TIX.Value = 0
    TIX.Parent = leaderstats
	
	local REDEEM = Instance.new("NumberValue")
	REDEEM.Name = "REDEEM"
	REDEEM.Value = 0
	REDEEM.Parent = player
    
	local data = nil
	local data2 = nil
	local success, errormessage = pcall(function()
		--task.spawn(function()
		----	game:GetService("ServerStorage"):FindFirstChild(player.Name.. " Stuff").IsLoaded.Value = true
		--end)
		data = playerData:GetAsync(player.UserId.."-TIX", player.leaderstats.TIX.Value) 
		data2 = playerData:GetAsync(player.UserId.."-REDEEM", player.REDEEM.Value) 
    end)

    if success and data ~= nil and data2 ~= nil then 
		TIX.Value = data
		REDEEM.Value = data2 
    else
        player:Kick("DATA COULDNT LOAD PLEASE REJOIN")
        warn(errormessage)
    end
end)
1 Like

Don’t kick players for this, it’s because they haven’t been assigned a datastore yet as they are joining for the first time.

2 Likes

If it’s nil that means the key has t been written on yet. Give them the default value.

So just replace data tables with 0 instead of nil?

No, I mean to set the leader stats values instead of kicking them.

game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
playerData:SetAsync(player.UserId…"-TIX", player.leaderstats.TIX.Value)
playerData:SetAsync(player.UserId…"-REDEEM", player.REDEEM.Value)
end)

if not success then
    warn(errormessage)
end

end)

It’s not successful because the value is nil.
You basically add the player data when the player is added, when the player is removed, you remove the player data.
This is the right way to do it. So if you want to save the data, you should make sure that the data is not nil.

1 Like