I Do Not Know How To Finish My Script SaveData

Hello! I need help finishing my script for SavaData.
Here is my script:

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)

Here is my leaderstat script

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
        local bigness = Instance.new("IntValue")
	bigness.Name = "Bigness"
	bigness.Value = 500
	bigness.Parent = leaderstats

end)
2 Likes

You could literally just encase all of that inside 1 script for a full on DataStore script

You forgot to end your pcall() function

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
    end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)
1 Like

When i did this i got a million a few errors

Surely that can’t be your entire script though

You must’ve syntax’ed something wrong if that’s the case

what do you mean?

(Ignore This. It Is The 3 0 Character Thing)

Wait I see the error facepalm

You forgot to make the pcall a function

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
    end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)
1 Like

So this got rid of the errors but it does not work. Do you want me to show the leaderstats code?

Yes please, also do show the Output if you get anything back

Especially the pcall results

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
        local bigness = Instance.new("IntValue")
	bigness.Name = "Bigness"
	bigness.Value = 500
	bigness.Parent = leaderstats

end)

Let me get all of the output results

i am getting no output responses @JackscarIitt

Hm, the SaveData script should be located inside ServerScriptService correct? There might be a rare chance that you may need to use BindToClose() instead

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
    end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)

game:BindToClose(function()
    for _, Player in pairs(game.Players:GetPlayers()) do
        local success, errormessage = pcall(function()
		    myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
        end)

        if success then
	    	print("Player Data Successfully Saved when the game was about to shutdown!")
	    else	
	    	print("There Was An Erroer When Saving Data")
	    	warn(errormessage)	
	    end
    end

end)

Ok let me try this. Ye it is in SSS

Should we do this
game:BindToClose(function(player)
instead of
game:BindToClose(function()

Bruh you should’ve implemented that yourself though

It’s literally your script

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
    end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)

game:BindToClose(function()
    for _, Player in pairs(game.Players:GetPlayers()) do
        local success, errormessage = pcall(function()
		    myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
        end)

        if success then
	    	print("Player Data Successfully Saved when the game was about to shutdown!")
	    else	
	    	print("There Was An Erroer When Saving Data")
	    	warn(errormessage)	
	    end
    end

end)
1 Like

so should we do this? (We need to definded “player” in game:BindToClose(function()

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
	end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end

	game:BindToClose(function()
		for _, Player in pairs(game.Players:GetPlayers()) do
			local success, errormessage = pcall(function()
				myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Vaule)
			end)

			if success then
				print("Player Data Successfully Saved when the game was about to shutdown!")
			else	
				print("There Was An Erroer When Saving Data")
				warn(errormessage)	
			end
		end
	end)
end)	

Don’t encase it inside the PlayerRemoving event

It should be its own separate function, as it will fire as soon as the game unexpectedly closes

You can clearly see “Value” was misspelled in your code. Bigness.Vaule

2 Likes

so how would we define player then??
game:BindToClose(function(player)