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

How do I save a DataStore in a :BindToClose() function? [Solved]

Hello developers, I'm unsure how to save data in :BindToClose(). I've tried ways such as:

game:BindToClose(function()
	local Player = game:GetService("Players"):GetPlayers()
	local playerid = "Player_"..Player.UserId
	local data = Player.leaderstats.deaths.Value

	local success, errormessage = pcall(function()
		HisDataStore:SetAsync(playerid, data)
	end)

	if success then
		print("Data has been successfully saved!")
	else
		print("Data has not been saved, negative!")
		warn(errormessage)
	end
end) 

I've tried this way but doesn't work. Gives the error 'attempt to concatenate string with nil' on the line local playerid = "Player_"..Player.UserId

I can see the mistake but I am rather confused how to fix it.

1 comment

0

TIP: When a question is solved by you, it's good to write [Solved] or [Answered] at the front or end of the question, so that it won't be marked as unanswered.

1 answer

I found out the answer. I used a for loop to iterate the players.

Code:

game:BindToClose(function()
    local Player = game:GetService("Players")
    for _, players in pairs(Player:GetPlayers()) do
        local playerid = "Player_"..players.UserId
        local data = players.leaderstats.deaths.Value

        local success, errormessage = pcall(function()
            HisDataStore:SetAsync(playerid, data)
        end)

        if success then
            print("Data has been successfully saved in studio!")
        else
            print("Data has not been saved, negative!")
            warn(errormessage)
        end
    end
end)

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