What is wrong with this data store saving code

Slaughters value doesn’t seem to save. Help would be appreciated.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SlaughterCount")

game.Players.PlayerAdded:Connect(function(Player)
	local string = Instance.new("IntValue", Player)
	string.Name = "Slaughters"
	string.Value = 0
	local Coins = Instance.new("IntValue", Player)
	Coins.Name = "Coins"
	local Vote = Instance.new("IntValue", Player)
	Vote.Name = "Vote"
	local Data
	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId.."-Slaughters")
	end)
	wait(1)
	if success then
		if Data then
			print(Data) -- always prints 0
			Player:WaitForChild("Slaughters").Value = Data
		else
		end
	else
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local SaveData = Player:WaitForChild("Slaughters").Value
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(Player.UserId.."-Slaughters", SaveData)
	end)
end)

on the save data code have you checked if it does save like do a print() if success and if not print the error message

Oh wait I just noticed why is the variable for the Slaughters Int value the function “string”? I’m sure that it would return an error try replacing it with other names like “Slaughter”

Does the same thing. And yes I have tried printing, nothing errors and the value is saved as 0

My guess is that the IntValue Slaughters is removed before the script can retrieve the value due to you parenting it under the Player Instance, while you are attempting to save it when the PlayerRemoving event fires.

One solution is to create a table that will hold your players information, and save the value from this table instead.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SlaughterCount")
local PlayerData = {}

local function freeSlaughter(Player)
    local pdata = PlayerData[Player]
    if pdata then
        pdata += 1
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    local Data
	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId.."-Slaughters")
	end)
    if Data then
        PlayerData[Player] = Data
        print(Data)
    end
    freeSlaughter(Player)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local SaveData = PlayerData[Player]
    if SaveData then
        local success, errorMessage = pcall(function()
		    DataStore:SetAsync(Player.UserId.."-Slaughters", SaveData)
	    end)
    end
    PlayerData[Player] = nil
end)

cuz you defined the slaughter as a global : (((((((

Make the variable to “slaughter”

Something like this? It doesn’t seem to be working.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SlaughterCount")
local PlayerData = {}

game.Players.PlayerAdded:Connect(function(Player)
	local string = Instance.new("IntValue", Player)
	string.Name = "Slaughters"
	string.Value = 0
	local Coins = Instance.new("IntValue", Player)
	Coins.Name = "Coins"
	local Vote = Instance.new("IntValue", Player)
	Vote.Name = "Vote"
	local Data
	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId.."-Slaughters")
	end)
	if Data then
		PlayerData[Player] = Data
		print(Data)
	end
	Player.Slaughters:GetPropertyChangedSignal("Value"):Connect(function(Value)
        print("check") -- never prints
		PlayerData[Player] = Value
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	print(PlayerData[Player]) -- always prints "nil"
	local SaveData = PlayerData[Player]
	if SaveData then
		local success, errorMessage = pcall(function()
			DataStore:SetAsync(Player.UserId.."-Slaughters", SaveData)
		end)
	end
	PlayerData[Player] = nil
end)

Wait nevermind. I was stupid. I used an event system and it worked. Thanks!