Data not working for no reason

I want to save the players exp and level but it’s not working and it doesn’t show any warning massage not even in script analysis. Help ?

    local PlayerId = "Player_"..Player.UserId
    
    --LoadData
    
    local data = {Exp.Value, Level.Value}
    local success, errormassage = pcall(function()
        data = ExpDataStore:GetAsync(PlayerId)
    end)
    
    
    if success then
        if data then
            Exp.Value = data.Exp
            Level.Value = data.Level
            -- Set out data equal to the current exp
        end
    end
    
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local PlayerId = "Player_"..Player.UserId
    
    local data = {
        Exp = Player.leaderstats.Exp.Value;
        Level = Player.leaderstats.Level.Value;
    }
    
    local success, errormassage = pcall(function()
        ExpDataStore:SetAsync(PlayerId, data)
    end)
    
    if success  then
        print("Data Saved")
    else
        print("Error saving data")
        warn(errormassage)
    end
end)```
1 Like
local PlayerId = "Player_"..Player.UserId

--LoadData

local data = {Exp.Value, Level.Value}
local success, errormassage = pcall(function()
	data = ExpDataStore:GetAsync(PlayerId)
end)


if success then
	if data then
		Exp.Value = data.Exp
		Level.Value = data.Level
		-- Set out data equal to the current exp
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerId = "Player_"..Player.UserId
	local data = {
		Exp = Player.leaderstats.Exp.Value;
		Level = Player.leaderstats.Level.Value;
	}

	local success, errormassage = pcall(function()
		ExpDataStore:SetAsync(PlayerId, data)
	end)
	if success  then
		print("Data Saved")
	else
		print("Error saving data")
		warn(errormassage)
	end
end)

game:BindToClose(function()
	if game:GetService('RunService'):IsStudio() then
       wait(1)
    end
end)

are you testing in studio?
because in studio, when the player leaves, the game does not get enough time to save data.
(try the above script, i added a :BindToClose() Function which Waits if your testing in studio.

2 Likes