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

Cant get Save Script to save stuff?

Im trying to make my simulator save but my code is not working no errors show up when tested.

local DS = game:GetService("DataStoreService"):GetDataStore("SavaData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Crumbs
	local save2 = plr.leaderstats.Coins
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		DS:GetAsync(plrkey,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId,{plr.leaderstats.Crumbs.Value, plr.leaderstats.Coins.Value})
end)

2 comments

0

what exactly isnt working?

0

The values that are needing to be saved are returning to beggining value

2 answers

✓ Accepted answer

Improvements:

  • When indexing a player's user id use plr.UserId not plr.userid
  • It's better to put the leaderstats and the datastore in the same script so that it saves time and uses fewer lines.

Those are the only errors/bugs I can see in your code.

Pro-tip:

When debugging code, use print statements to help you know what lines are running and what lines aren't.

Make sure this is a Normal Script.

(Also, make sure your API service is on. You can check this by going on game settings < Security and then enable it.)

Fixed Script:

local DS = game:GetService("DataStoreService"):GetDataStore("SavaData")
	
game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder",player)
	Folder.Name = "leaderstats" 

	local Coins = Instance.new("IntValue",Folder)
	Coins.Name = "Coins"

	local Crumbs = Instance.new("IntValue",Folder)
	Crumbs.Name = "Crumbs"

	local plrkey = "id_"..player.UserId
	local save1 = player.leaderstats.Crumbs
	local save2 = player.leaderstats.Coins

	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		DS:GetAsync(plrkey,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.UserId,{plr.leaderstats.Crumbs.Value, plr.leaderstats.Coins.Value})
end)

##Recommendation(s):

It's also better to use DataModel:BindToClose, as this function gets called when the game shuts down. <-------------------------------------------------------------------------------------------------------->

Re-edited: (I also added another variable that references the Datastore, I also added pcalls to ensure the code runs through any errors.)

Note: You don't need to add BindToClose on line 30, you could've added it on the bottom of the script, but I just decided to do it like this:

##Re-edited Script:

local DS = game:GetService("DataStoreService")
local myDataStore = DS:GetDataStore("SaveData") -- Added

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder",player)
	Folder.Name = "leaderstats" 

	local Coins = Instance.new("IntValue",Folder)
	Coins.Name = "Coins"

	local Crumbs = Instance.new("IntValue",Folder)
	Crumbs.Name = "Crumbs"

	local data
	local playerUserId = "Player__" .. player.UserId	
	local success,errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)

	end)
	if success then
		if data then
			Coins.Value = data.Coins
			Crumbs.Value = data.Crumbs

		end
	end
end)


	game:BindToClose(function()
		print("The games has shutdown")

		for i,plr in pairs(game.Players:GetPlayers()) do
			local data = {
				Coins = plr.leaderstats.Coins.Value;
				Crumbs = plr.leaderstats.Crumbs.Value;
			}
			print("it works")
		end
		
	end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player__" .. player.UserId
	local data = {
		Coins = player.leaderstats.Coins.Value;
		Crumbs = player.leaderstats.Crumbs.Value;
	}
		local success,errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId,data)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("Data failed to save!")
	end
end)
 

If my answer helped you, please accept it!

Fixed grammatical errors

~5/12/2021

##Reference:

DataModel:BindToClose

12 comments

0

More enquires or comments, post here.

0

what do i replace with DataModel:BindToClose?

0

im going to update my answer, wait

0

also, if my answer helped you, don't forget to accept it

0

Re-edited, check my answer

0

it is saving once but after i try to save it more than once it will just go back to the amount from the first save

0

if it helps the (regular) script is in serverscriptservice

0

include a video or a snapshot so i can give you assistance.

0

also, tell me what youre using to change the amount. for example, are you changing it server sided or client sided

0

this script should work. if it doesn't i will review the code later, but i think ur doing something wrong

0

if you have more questions comment at 10 AM (GMT)

0

oops didnt copy a couple of lines on accident now it works thanks :)

If there aren't errors, then its possible this won't work because your using studio. When a player leaves, it fires player removing before the server can shut down, this is normal roblox. But with studio, when you hit the stop button, it shuts down the virtual server so it won't actually run player removing most of the time. I have experienced it myself this week and many times before. To make sure it saves, you can save it manually while using studio, make a button or something which upon clicking, will fire to a remote event that saves the data before you click stop which closes the virtual server.

Hope this helps :3

3 comments

0

ever heard of bindtoclose

0

No

0

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