Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] Why is new player data being overwritten with 0?

Hello! I'm pretty new to datastores and I'm currently having a problem where the initial value (1) created in a new player seems to be overwritten with 0 even though there was no data to begin with. There's probably an obvious solution to this, but I can't figure out why this is happening. There are no errors.

local DataStoreService = game:GetService("DataStoreService")

local SkipData = DataStoreService:GetDataStore("Skips")

game.Players.PlayerAdded:Connect(function(player)
	
	local Skips = Instance.new("IntValue")
	Skips.Name = "Skips"
	Skips.Parent = player
	Skips.Value = 1 -- initial value

local data
local success, errormessage = pcall(function()
	data = SkipData:GetAsync(player.UserId.."-Skips")
end)

	if success then
		Skips.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end	
end)

game:BindToClose(function()
	if not game:GetService("RunService"):IsStudio() then
		for i, player in pairs(game.Players:GetPlayers()) do
			local success, errormessage = pcall(function()
			SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)	
		end)
		if not success then
			warn(errormessage)
		end
		end
	end
end)

Thanks in advance!

5 comments

0

You could try printing the leaderstat value and the data given from the datastore and see the difference to check if you set the value correctly. You code looks ok other than that.

0

You could do: Skips.Value = SkipData:GetAsync(player.UserId.."-Skips") or 1

0

Plus pcall it ^

0

This seems to work! Thanks!

0

No problem!

1 answer

✓ Accepted answer

Rather than setting the value twice, you can set it to the value saved in a DataStore. If it does not get data at all, it will set the value to the specified number. I provided the code below:

local DataStoreService = game:GetService("DataStoreService")

local SkipData = DataStoreService:GetDataStore("Skips")

game.Players.PlayerAdded:Connect(function(player)

    local Skips = Instance.new("IntValue")
    Skips.Name = "Skips"
    Skips.Parent = player
    pcall(function()
    	Skips.Value = SkipData:GetAsync(player.UserId.."-Skips") or 1
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)
    end)

    if success then
        print("Player Data successfully saved!")
    else
        print("There was an error when saving data")
        warn(errormessage)
    end 
end)

game:BindToClose(function()
    if not game:GetService("RunService"):IsStudio() then
        for i, player in pairs(game.Players:GetPlayers()) do
            local success, errormessage = pcall(function()
            SkipData:SetAsync(player.UserId.."-Skips", player.Skips.Value)  
        end)
        if not success then
            warn(errormessage)
        end
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.