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

Simple Datastore Save Script Not Working?

I Have been trying to figure this out all day an I swear I'm going to have a mental breakdown by the end of the day

so its a VERY VERY basic datastore saving script that is not not working for some odd reason

game.Players.PlayerRemoving:Connect(function(Player)
	
	local success, errormessage = pcall(function() -- Pcall
		KnifeData:SetAsync(tostring(Player.UserId).."-THKnive", game.ServerStorage.PlayerInfo:FindFirstChild(Player.Name):FindFirstChild("ThrownKnives").Value) -- Gets the value from a IntValue that is created when the player joins
		wait() -- Waits to clean up
		game.ServerStorage.PlayerInfo:FindFirstChild(Player.Name):Destroy() -- Cleans Up
	end)

	if success then -- It saved. Indicate that.
		print("Saved "..Player.Name.."'s KniveData")
	else
		warn("Error While Saving "..Player.Name.."'s Data")
		warn(errormessage)
	end

end)

this will sometimes work but most of the time not and it is not reliable

I swear if this is very simple I will be so mad. Or glad. Depends

1 answer

✓ Accepted answer

I am sure you were testing this in studio because you said "this will sometimes work but most of the time not and it's not reliable"

Well, server in roblox studio shutdowns/closes as you stop the client, giving no time for the server to react to your .PlayerRemoving event.

You must use game:BindToClose() function to test this in studio and to prevent data loss after a shutdown in your actual game ( online server ).

First move your whole saving script inside a function called anything you want

local function save(Player)
    -- your code here...
end)

Next, make a .PlayerRemoving event and game:BindToClose() function

game.Players.PlayerRemoving:Connect(function(player)
    -- CALL YOUR FUNCTION HERE for example save(player)
end)

game:BindToClose(function() -- this only runs when server closes...
    for _, player in pairs(game.Players:GetPlayers()) do
        -- CALL YOUR FUNCTION HERE for example save(player)
    end
end)

ALSO to prevent datastore being overwhelmed in studio do this thing for your game:BindToClose function: ( OPTIONAL )

game:BindToClose(function() -- this only runs when server closes...
    if game:GetService("RunService"):IsStudio() then
        return -- stops the function if its running in studio
    else
        for _, player in pairs(game.Players:GetPlayers()) do
            -- CALL YOUR FUNCTION HERE for example save(player)
        end
    end
end)

1 comment

0

Seems like this works. Thanks!

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