#Question about the PlayerRemoving Event

1 messages · Page 1 of 1 (latest)

primal hamlet
#

I am trying to learn about the DataStores and "leaderstats" and a part of that is saving values when the player is quitting the game, so i created a simple script that prints out when the player leaves the value of his "Cash" (a instance inside of the player inside of the leaderstats folder). The problem is it always prints Zero and idunno why. Here is the code and a screenshot of my workspace, thanks for the help guys <3

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Value = 0
    cash.Parent = leaderstats
end)


players.PlayerRemoving:Connect(function(player)
    local playerStatsFolder = player:FindFirstChild("leaderstats")
    local cash = playerStatsFolder:FindFirstChild("Cash")
    print("Cash of " .. player.Name .. " is " .. cash.Value)
end)
orchid oriole
#

Make sure to press this and switch to the server before editing the value

timid yew
#

uhh

#

you do know that it doesn't save if you don't actually use the datsstore right

orchid oriole
#

oh shoot

#

I didn't even notice it wasn't there lol

timid yew
#

yeah its gotta be that 100%

primal hamlet
#

i dont even have datastore in this script

#

i am talking about the variable printing 0

#

it wasnt set to 0

#

and because of that the datastore wasnt working to

#

so i tried it with this simple script first

timid yew
#

oh

#

hm

#

try Kruz's method

#

just switch to server while testing

#

and then u set the value before exiting

primal hamlet
#

got it now running

#

but how do i change the value correctly it needs to be done serverside not clientside right?

#

so with a script and not a local script?

#

im kinda new to script so yeah

#

unsure

orchid oriole
#

Yeah

primal hamlet
#

i thought about firing a remoteevent

#

?

#

but dunno how

#

is that the right way kinda?

orchid oriole
#

I wouldn't use a remote event to set any save data

primal hamlet
#

i would use it to change the value player/leaderstats/Cash.Value

#

it then saves the leaderstat when the player quits

#

its just a place to get warmed up with datastorage and client/serverside scripting

orchid oriole
#

Anything involving setting data or values to be saved should be handled with server scripts (for the most part) to prevent exploiting, but since you're just using it for practice, I guess using a remoteevent wouldn't be too harmful