How to make pcall datastore?

Hi, i want make datastore using pcall function pls help script:

local DataStoreService = game:GetService("DataStoreService")
local Goldtraildatastore = DataStoreService:GetDataStore("GoldTrailDatastore")

game.Players.PlayerAdded:Connect(function(Player)
	local GoldTrail = Instance.new("IntValue")
	GoldTrail.Parent = Player:WaitForChild("Trails")
	GoldTrail.Name = "GoldTrail"
	GoldTrail.Value = 0
	local Data = Goldtraildatastore:GetAsync(Player.UserId)
	if Data then
		GoldTrail.Value = Data
	end
end)
--------------------------------Autosaving part----------------------------------------
game.Players.PlayerRemoving:Connect(function(Player)
	Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
end)
1 Like

This should help Pcalls - When and how to use them

local DataStoreService = game:GetService("DataStoreService")
local Goldtraildatastore = DataStoreService:GetDataStore("GoldTrailDatastore")

game.Players.PlayerAdded:Connect(function(Player)
	local GoldTrail = Instance.new("IntValue")
	GoldTrail.Parent = Player:WaitForChild("Trails")
	GoldTrail.Name = "GoldTrail"
	GoldTrail.Value = 0
	local success,err = pcall(function()
 
 Goldtraildatastore:GetAsync(Player.UserId)
   end)
	if Data then
		GoldTrail.Value = Data
	end
end)
--------------------------------Autosaving part----------------------------------------
game.Players.PlayerRemoving:Connect(function(Player)
	Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
end)

something like this i guess

Try this out:

local DataStoreService = game:GetService("DataStoreService")
local Goldtraildatastore = DataStoreService:GetDataStore("GoldTrailDatastore")

game.Players.PlayerAdded:Connect(function(Player)
	local GoldTrail = Instance.new("IntValue")
	GoldTrail.Parent = Player:WaitForChild("Trails")
	GoldTrail.Name = "GoldTrail"
	
	local Success, Error
	
	Success, Error = pcall(function()
		Data = Goldtraildatastore:GetAsync(Player.UserId)
	end)
	
	if Success then
		if Data then
			GoldTrail.Value = Data
			print("Loaded "..Player.UserId)
		else
			GoldTrail.Value = 0
			print(Player.Name.." is new to the game")
		end
	else
		print(Error)
	end
end)
--------------------------------Saving part----------------------------------------
game.Players.PlayerRemoving:Connect(function(Player)
	local success, errorMsg
	
	success, errorMsg = pcall(function()
		Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
	end)
	
	if success then
		print("Saved "..Player.UserId)
	else
		print(errorMsg)
	end
end)

it dont saved my stats, but no errors

Did it print out anything in the output?

Screenshot_6
only that

This means that it’s loading, but I don’t know about the saving part.

this dont saving for me. Not errors

local DataStoreService = game:GetService("DataStoreService")
local Goldtraildatastore = DataStoreService:GetDataStore("GoldTrailDatastore")

game.Players.PlayerAdded:Connect(function(Player)
	local GoldTrail = Instance.new("IntValue")
	GoldTrail.Parent = Player:WaitForChild("Trails")
	GoldTrail.Name = "GoldTrail"
	GoldTrail.Value = 0

	xpcall(function()
        GoldTrail.Value = Goldtraildatastore:GetAsync(Player.UserId) or 0
    end, warn)
end)
--------------------------------Autosaving part----------------------------------------
game.Players.PlayerRemoving:Connect(function(Player)
    local success = xpcall(function()
	   Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
    end, warn)

    if success then
        print('Saved Data')
    end
end)

this don`t saved data for me. only prints: Saved Data

local DataStoreService = game:GetService("DataStoreService")
local Goldtraildatastore = DataStoreService:GetDataStore("GoldTrailDatastore")

game.Players.PlayerAdded:Connect(function(Player)
	local GoldTrail = Instance.new("IntValue")
	GoldTrail.Parent = Player:WaitForChild("Trails")
	GoldTrail.Name = "GoldTrail"
	GoldTrail.Value = 0
	local Data = Goldtraildatastore:GetAsync(Player.UserId)
	if Data then
		GoldTrail.Value = Data
	end
end)
--------------------------------Autosaving part----------------------------------------
game.Players.PlayerRemoving:Connect(function(Player)
	local Success,Fail = pcall(function()
		Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
	end)
	
	if Fail then
		Goldtraildatastore:SetAsync(Player.UserId, Player.Trails.GoldTrail.Value)
	end
end)

wat id do

you should say how you did it man

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.