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

After i rejoin game all my values are 0?

After i rejoin my game, all my values go 0, please help me.

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("FruitSimulatorDataStore02")

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", game.ReplicatedStorage)
	stats.Name = plr.Name.."Stats"
	local coins = Instance.new("NumberValue",stats)
	coins.Name = "Coins"
	coins.Value = data:GetAsync(plr.UserId) or 0
	local baglvl = Instance.new("NumberValue",stats)
	baglvl.Name = "Bag"
	baglvl.Value = data:GetAsync(plr.UserId) or 15
	local fruits = Instance.new("NumberValue", stats)
	fruits.Name = "Fruits Collected"
	fruits.Value = data:GetAsync(plr.UserId) or 0
	local world2unlocked = Instance.new("NumberValue",stats)
	world2unlocked.Name = "W2Unlocked"
	world2unlocked.Value = data:GetAsync(plr.UserId) or 0
end)

game.Players.PlayerRemoving:Connect(function(player)
	data:SetAsync(player.UserId, game.ReplicatedStorage[player.Name.."Stats"].Coins.Value, game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value, game.ReplicatedStorage[player.Name.."Stats"].Bag.Value, game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value)
    print("Saved "..player.Name.."'s Stats.")
end)

4 comments

0

I dont know if this would even work or make it better but try making the values save into a folder that is added into the player when they join containing all of their stats. Im not to good with save scripts so i dont know much about them so this is what i though of>

0

Because you have placed the folder in game.ReplicatedStorage

0

@adieking1 The reason is not because it is in the ReplicatedStorage.

0

@imKlevi FYI the parent argument for Instance.new is deprecated. Just use instance.Parent = Parent

2 answers

Your script looks fine; however, I would advise you to save your values in a table as there is a limited request count with saving multiple values in one data store. Now, I will address what I think is your problem. Try adding the :BindToClose function to your script because there are times when the server shuts down before the last player's data is saved. This could also happen while you're playing solo in the Studio. :BindToClose will allow all your remaining functions (including PlayerRemoving) to run less than 30 seconds before the server shuts down. Simply add this script to yours at the end:

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do -- loops through all the players in the server
	
	local values = {
game.ReplicatedStorage[player.Name.."Stats"].Coins.Value, 						game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value, 	game.ReplicatedStorage[player.Name.."Stats"].Bag.Value, 	game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value
}

    		data:SetAsync(player.UserId, values)
		print("Saved "..player.Name.."'s Stats.")

	end
end)

5 comments

0

I should use BindToClose instead of PlayerLeaving ?

0

Not working.

0

I told you to add this script to your original. Don't remove anything from your original script.

0

Okay

0

Yeah but bag value before save it's 15, after save it goes same as "Fruits Collected" value.

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