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

Weird problem with PlayerRemoving? [CLOSED] [ANSWERED]

I'm trying to do a DataStorage code and the PlayerRemoving function just doesnt work. Output: 18:05:49.960 - Workspace.leaderstatsSave:36: attempt to call field 'PlayerRemoving' (a userdata value)

Code:

game.Players.PlayerRemoving(function(p)
	--SAVE--
	local DataStore = game:GetService("DataStoreService"):GetDataStore(p.userId)
	local stats = p.leaderstats:GetChildren()
	local s1 = {}
	for i=1,#stats do
		local n = stats[i].Name
		s1[n] = stats[i].Value
	end
	DataStore:SetAsync("stats",s1)
	--print("Saved "..p.Name)
end)

Help?

2 answers

✓ Accepted answer
game.Players.PlayerRemoving:connect(function(p) -- you need to connect the listener to a function
    --SAVE--
    local DataStore = game:GetService("DataStoreService"):GetDataStore(p.userId)
    local stats = p.leaderstats:GetChildren()
    local s1 = {}
    for i=1,#stats do
        local n = stats[i].Name
        s1[n] = stats[i].Value
    end
    DataStore:SetAsync("stats",s1)
    --print("Saved "..p.Name)
end))

1 comment

0

Thanks for reminding me, I completely forgot to add :connect in front of function.

MINOR MISTAKE ONLY THIS WONT BE A LENGTHY ANSWER

or will it????

You cannot call PlayerRemoving like a function, but you can use :connect on it, which you aren't.

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