How would i use pcalls?

i’m making a screen where you can attempt to fix the game. here is my code:
image
would this work?

No, you would have to put the actual content of the function in a pcall and check if that runs. Paste your script here and I can fix it.

You would wrap the pcall inside the event, not outside

P.S. you should copy and paste the code, not screenshot it


local storage = game:GetService("ServerStorage")

local Map1 = workspace.Map1
local Map2 = storage.Map2

script.Parent.Touched:Connect(function(hit)
   local success, err = pcall(function()
       if game.Players:GetPlayerFromCharacter(hit.Parent) then
           Map1.Parent = storage
           Map2.Parent = workspace
           print("loaded map2")
       end
   end)

   if not success then
       warn(err) -- Send the reason for failure to the output log
       --...
   end
end)

Though, I’m confused about what you’re trying to accomplish here, as pcall doesn’t seem necessary

1 Like

local Map1 = game.Workspace.Map1

local Map2 = game.ServerStorage.Map2

local succ, err = pcall(function()

script.Parent.Touched:Connect(function(hit)

if game.Players:FindFirstChild(hit.Parent.Name) then

Map1.Parent = game.ServerStorage

Map2.Parent = game.Workspace

print (“loaded map2”)

end

end)

end)

if not succ then

print(“MapLoadingScript didn’t run.”)

game.StarterGui.DebugScreen.DebugFrame.Visible = true

game.StarterGui.UIDisable.Disabled = true

Map1.Parent = game.ServerStorage

Map2.Parent = game.ServerStorage

end

On a side note, don’t use StarterGui as that is only for giving the player UI. Use Player.PlayerGui

i don’t understand why because it still works

Change your script to this:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--//Variables
local Map1 = workspace.Map1
local Map2 = ServerStorage.Map2
local Part = script.Parent

--//Functions
Part.Touched:Connect(function(hit)
	local success, errorMessage = pcall(function()
		local Player = Players:GetPlayerFromCharacter(hit.Parent)

		if not Player then
			return
		end
		
		Map1.Parent = ServerStorage
		Map2.Parent = workspace
	end)
	
	if success then
		print("loaded map2")
	else
		warn(errorMessage)
		print("MapLoadingScript didn’t run.")
		
		for i, player in ipairs(Players:GetPlayers()) do
			task.spawn(function()
				local playerGui = player:WaitForChild("PlayerGui")
				playerGui.DebugScreen.DebugFrame.Visible = true
				playerGui.UIDisable.Disabled = true
			end)
		end

		Map1.Parent = ServerStorage
		Map2.Parent = ServerStorage
	end
end)

The only reason StarterGui worked was because the script ran before all the player’s guis were loaded in so it just loaded the gui from StarterGui.

1 Like

I’ll try this tomorrow. Looks promising. I want to use pcalls for the important scripts i have (loading, saving, important to progress etc) because i usually make them server sided so in case for some reason something goes wrong, i can make the debug screen visible where you can choose to restart all scripts, rejoin or close the screen (I’m not sure why i added that option because restarting scripts would probably be better. I think I’ll remove that option. Restarting scripts is basically not wanting to lose your progress, not wanting to rejoin or if teleport service is down. Rejoining is if you want to make sure the game fixes itself)

You shouldn’t even be using pcall for this, if the code errors then that’s an issue with the code not because the code is prone to errors (like network API methods).

1 Like

Didn’t work. i tried to make the script error on purpose by making a typo but nothing happened.