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

What is wrong with my DataStore code? Things aren't getting saved all the time

My game is not saving items and currency every single time. I notice that sometimes it saves the currency when leaving, other times it does not, it does not save any items or the leaderstats I buy in my game AT ALL. Why is this? My scripts are fine right?

This script is for when players join a check. It checks their money, level, and if they bought an item (when buying the item, it makes the int value, hp equal to 1)

local DataStore = game:GetService("DataStoreService"):GetDataStore("mymdata")

game.Players.PlayerAdded:connect(function(player)	
	local leaderboardstats = Instance.new("Model", player)
	leaderboardstats.Name = "leaderstats"
	local status = Instance.new("IntValue")
	status.Parent = leaderboardstats
	status.Name = "Money"
	print("Getting money value for player")
	local levels = Instance.new("IntValue")
	levels.Parent = leaderboardstats
	levels.Name = "Level"
	local healing = Instance.new("IntValue", player)
	healing.Name = "hp" -- Healing Potion
	local key = "player-"..player.userId
	local savedValues = DataStore:GetAsync(key)
	if savedValues then
		status.Value = savedValues[1] 
		levels.Value = savedValues[2] 
		healing.Value = savedValues[3]
	else
		local valuesToSave = {status.Value, levels.Value, healing.Value}		
		DataStore:SetAsync(key, valuesToSave)						
	end	
	if healing.Value == 1 then
	print('healing is true')
	local item = game.StorageServer.Potion  ---ITEMHERE
	item:clone().Parent = player.StarterGear
	item:clone().Parent = player.Backpack	
	else 
	wait()
end	 
end)


Now I have this script for when players leave

local DataStore = game:GetService("DataStoreService"):GetDataStore("mydata")

game.Players.PlayerRemoving:connect(function(player)
	local key = "player-"..player.userId
	local valuesToSave = {player.leaderstats.Coins.Value, player.leaderstats.Level.Value, player.hp.Value}	
	DataStore:SetAsync(key, valuesToSave)	
	print("Saved to data store")
end)

2 answers

When the event 'PlayerRemoving' is called, the player is immediately leaving, and because of the usual server-client lag and all that, your PlayerLeaving script will not be so effective.

May I offer a solution:

Rather than saving player values inside the player, add a DataFolder is something like Lighting, and save the values in there - that means that the values are still there even when the player is leaving, so lag doesn't need to be counted. (To add on, delete the PlayerData Folder after you have saved it all to make life easier)

Be sure to always look in the output window, it will help you work out your solution - the way I found out the above solution in the first place.

Hopefully that has helped you solve your problem, If you require more information just ask, Good luck.

~~ Please upvote if I solved your answer! ~~

1 comment

0

As this is most likely a server-Script, the latency should not matter (by the time the server knows a player is disconnecting they are already disconnecting). Anyway, I would be suprised if PlayerRemoving is called after the Player object has already been removed (it is RemovING, after all).

You are saving to "mydata" but loading from "mymdata"

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