Safezone script

I follown a tutorial for make a safezone but it didnt work,I think that the problrm is that the script is not proprerly connected to the part I have made as a “safezone”.I tried to find errors in the script but I didnt find anything , there are 2 scripts ,one called safezone ,this should be the script of the safezone itself and then another one called protect that is releated to the server ,events and other stuff.Can someone help me to find the problrm?

safezone script
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild(“PlayerGui”)
local safeZone = game.Workspace.SafeZone
local pos1,pos2 = (safeZone.Position - (safeZone.Size / 2)),(safeZone.Position +(safeZone.Size / 2))
local region = Region3.new(pos1,pos2)
local starterGui game:GetService(“StarterGui”)
local forceFieldEvent = game:GetService(“ReplicatedStorage”).Events.ForceField
local toggle = false

while true do wait (1)
local playersInsafeZone = game.Workspace:FindPartsInRegion3(region,game.Workspace.Terrain)

 for i, playersFound in pairs(playersInsafeZone) do
	if playersFound:FindFirstAncestor(player.Name) then
		if not toggle then
			toggle = true
		end
	else
		toggle = false	
	end
end

if toggle == true then
	starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)--//Disables the backpack
	char.Humanoid:UnequipTools()--//Unequips tools
	playerGui.SafeZoneGui.Enabled = true --//Enables the gui
	print(player.Name.."is in safe zone")
else
	starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)--//Enables the backpack
	 playerGui.SafeZoneGui.Enabled = true --//Enables the gui
	print(player.Name.."is not in safe zone")
end    
   
forceFieldEvent:FireServer(char,toggle)

end
protect script
local forceFieldEvent = game:GetService(“ReplicatedStorage”).Events.Forcefield

forceFieldEvent.OnServerEvent:Connect(function(player,char,toggle)
if toggle == true then
if not char:FindFirstChild(“ForceField”) then
local forceField = Instance.new(“ForceField”,char)
else
print (player.Name…" alread has force field")
end
else
if not char:FindFirstChild (“ForceField”) then
print (player.Name…" has no force field")
else
char:FindfirstChild(“ForceField”):Destroy()
end
end
end)

1 Like
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild(“PlayerGui”)
local safeZone = game.Workspace.SafeZone
local pos1,pos2 = (safeZone.Position - (safeZone.Size / 2)),(safeZone.Position +(safeZone.Size / 2))
local region = Region3.new(pos1,pos2)
local starterGui game:GetService(“StarterGui”)
local forceFieldEvent = game:GetService(“ReplicatedStorage”).Events.ForceField
local toggle = false

while true do wait (1)
local playersInsafeZone = game.Workspace:FindPartsInRegion3(region,game.Workspace.Terrain)

 for i, playersFound in pairs(playersInsafeZone) do
	if playersFound:FindFirstAncestor(player.Name) then
		if not toggle then
			toggle = true
		end
	else
		toggle = false	
	end
end

if toggle == true then
	starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)--//Disables the backpack
	char.Humanoid:UnequipTools()--//Unequips tools
	playerGui.SafeZoneGui.Enabled = true --//Enables the gui
	print(player.Name.."is in safe zone")
else
	starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)--//Enables the backpack
	 playerGui.SafeZoneGui.Enabled = true --//Enables the gui
	print(player.Name.."is not in safe zone")
end    
   
forceFieldEvent:FireServer(char,toggle)
end
protect script
local forceFieldEvent = game:GetService(“ReplicatedStorage”).Events.Forcefield

forceFieldEvent.OnServerEvent:Connect(function(player,char,toggle)
if toggle == true then
if not char:FindFirstChild(“ForceField”) then
local forceField = Instance.new(“ForceField”,char)
else
print (player.Name…" alread has force field")
end
else
if not char:FindFirstChild (“ForceField”) then
print (player.Name…" has no force field")
else
char:FindfirstChild(“ForceField”):Destroy()
end
end
end)

Your code but formatted, i will try to solve your issue, give me time.

2 Likes

Thank you really much,I think that there’s no connection to the phisical part in the script,but I’ll let you resolve the problem cause I’m still new to scripting

1 Like

Are there any errors? [30 chars]

1 Like

Where?
if you mean the script no,I didnt saw any errors on the script page

1 Like

I have a screenshot of the part if this can help you

1 Like

so what can I do for resolve that?I actually send 2 scripts but I forgotten to highlight the start of the other one ,but at the end of the first one you can see that another script started,it finish like that

end
PROTECT SCRIPT

Hope this can help you

2 Likes
2 Likes

Try this?:

--ServerScript in ServerScriptService
local forceFieldEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events").Forcefield

forceFieldEvent.OnServerEvent:Connect(function(player,char,toggle)
	if toggle == true then
		if not char:FindFirstChild("ForceField") then
			local forceField = Instance.new("ForceField",char)
		else
			print(player.Name.." alread has force field")
		end
	else
		if not char:FindFirstChild ("ForceField") then
			print(player.Name.." has no force field")
		else
			char:FindfirstChild("ForceField"):Destroy()
		end
	end
end)
-- LocalScript in StarterPlayerScripts
local plr = game.Players.LocalPlayer
plr.CharacterAdded:Connect(function(character)
	char = character
end)
local playerGui = plr:WaitForChild("PlayerGui")
local safeZone = game.Workspace.SafeZone
local pos1,pos2 = (safeZone.Position - (safeZone.Size / 2)),(safeZone.Position +(safeZone.Size / 2))
local region = Region3.new(pos1,pos2)
local starterGui game:GetService("StarterGui")
local forceFieldEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events").ForceField
local toggle = false

while true do 
	wait(1)
	local PlayersInSafeZone = game.Workspace:FindPartsInRegion3(region,game.Workspace.Terrain)

	for _, PlayerInSafeZone in pairs(PlayersInSafeZone) do
		if PlayerInSafeZone:FindFirstAncestor(plr.Name) then
			if toggle == false then
				toggle = true
			end
		else
			toggle = false	
		end	
	end

	if toggle == true then
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)--//Disables the backpack
		char.Humanoid:UnequipTools()--//Unequips tools
		playerGui.SafeZoneGui.Enabled = true --//Enables the gui
		print(plr.Name.."is in safe zone")
	else
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)--//Enables the backpack
	 	playerGui.SafeZoneGui.Enabled = true --//Enables the gui
		print(plr.Name.."is not in safe zone")
	end    
   
	forceFieldEvent:FireServer(char,toggle)
end
2 Likes

Thank you I’m going to try it!

1 Like

I thank you for the time you spent helping me

1 Like