There is a Weird bug in this script

so I asked someone to make me a Script where if you entered a Zone with a part it would Disable the Backpack and give you Infinite Health after .2 seconds

but there is a weird bug where if i enter the zone with a tool equipped after like a second it would revert Your health to 100 and you get your tools back which is something i didn’t want happening

here is the script listed

local forceField = script.Parent
local debounceTime = 0.2 -- Cooldown time in seconds
local exitCooldownTime = 0.5 -- Cooldown time when exiting the force field
local playerDebounces = {}

local function disableWeapons(player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		for _, tool in ipairs(backpack:GetChildren()) do
			tool.Parent = player.StarterGear -- Move all tools to StarterGear
		end
	end
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:UnequipTools() -- Unequip any equipped tools
		end
	end
end

local function enableWeapons(player)
	local starterGear = player:FindFirstChild("StarterGear")
	local backpack = player:FindFirstChildOfClass("Backpack")
	if starterGear and backpack then
		for _, tool in ipairs(starterGear:GetChildren()) do
			tool.Parent = backpack -- Move all tools back to Backpack
		end
	end
end

local function makeInvincible(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.MaxHealth = math.huge
		humanoid.Health = math.huge
	end
end

local function setHealthTo100(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.MaxHealth = 100 -- Reset max health to 100
		humanoid.Health = 100 -- Reset health to 100
	end
end

local function addHighlight(character)
	local existingHighlight = character:FindFirstChild("Highlight")
	if not existingHighlight then
		local highlight = Instance.new("Highlight")
		highlight.FillColor = Color3.fromRGB(255, 0, 0) -- color
		highlight.OutlineTransparency = 1 -- Hide outline
		highlight.Parent = character
	end
end

local function removeHighlight(character)
	local highlight = character:FindFirstChild("Highlight")
	if highlight then
		highlight:Destroy()
	end
end

local function setDebounce(player, cooldown)
	playerDebounces[player] = true
	wait(cooldown)
	playerDebounces[player] = false
end

local function onForceFieldTouched(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and not playerDebounces[player] then
		setDebounce(player, debounceTime)
		disableWeapons(player) -- Disable all weapons
		makeInvincible(character) -- Make the player invincible
		addHighlight(character) -- Add a highlight effect
	end
end

local function onForceFieldTouchEnded(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and not playerDebounces[player] then
		setDebounce(player, exitCooldownTime)
		enableWeapons(player) -- Enable all weapons
		setHealthTo100(character) -- Reset health to 100
		removeHighlight(character) -- Remove the highlight effect
	end
end

forceField.Touched:Connect(onForceFieldTouched)
forceField.TouchEnded:Connect(onForceFieldTouchEnded)

this is the issue in the video

2 Likes

Here the script I just fixed

local ForceField = script.Parent
local debounceTime = 0.2 -- Cooldown time in seconds
local exitCooldownTime = 0.5 -- Cooldown time when exiting the force field
local playerWasTouched = {}

local function MakeDebounce(player, Cooldown, Exit)
	local Start = os.clock()
	repeat wait()
	until os.clock()-Start > Cooldown or (Exit and not playerWasTouched[player] or playerWasTouched[player])
end

local function disableWeapons(player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		for _, tool in backpack:GetChildren() do
			tool.Parent = player.StarterGear -- Move all tools to StarterGear
		end
	end
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:UnequipTools() -- Unequip any equipped tools
		end
	end
end

local function enableWeapons(player)
	local starterGear = player:FindFirstChild("StarterGear")
	local backpack = player:FindFirstChildOfClass("Backpack")
	if starterGear and backpack then
		for _, tool in starterGear:GetChildren() do
			tool.Parent = backpack -- Move all tools back to Backpack
		end
	end
end

local function setDeath(character, Bool)
	local humanoid: Humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, Bool)
	end
end

local function addHighlight(character)
	local existingHighlight = character:FindFirstChild("Highlight")
	if not existingHighlight then
		local highlight = Instance.new("Highlight")
		highlight.FillColor = Color3.new(1, 0, 0) -- color
		highlight.OutlineTransparency = 1 -- Hide outline
		highlight.Parent = character
	end
end

local function removeHighlight(character)
	local highlight = character:FindFirstChild("Highlight")
	if highlight then
		highlight:Destroy()
	end
end

local function onForceFieldTouched(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and not playerWasTouched[player] then
		playerWasTouched[player] = true -- So player can't touch it again
		MakeDebounce(player, debounceTime, false)
		
		if playerWasTouched[player] then -- Check if player still in forcefield.
			disableWeapons(player) -- Disable all weapons
			setDeath(character, false) -- Make player cannot death
			addHighlight(character) -- Add a highlight effect
		end
	end
end

local function onForceFieldTouchEnded(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player and playerWasTouched[player] and character and hit.Name == 'Humanoi' then
		playerWasTouched[player] = false
		MakeDebounce(player, debounceTime, true)
		
		if not playerWasTouched[player] then -- Check if player exits.
			enableWeapons(player) -- Enable all weapons
			setDeath(character, true) -- Make player can death
			removeHighlight(character) -- Remove the highlight effect
		end
	end
end

ForceField.Touched:Connect(onForceFieldTouched)
ForceField.TouchEnded:Connect(onForceFieldTouchEnded)

I also fix a bug where people can abuse the forcefield system for infinite health

1 Like

i just tested the script 3 times and now when you enter the area it will only take all your weapons (except the one you equipped) and the health does not display “inf” at all, and also does not let you reset

this is the video listing the issue

Can you post a picture of the ForceField part in the world highlighted just so we know where exactly the Touched and TouchEnded events would trigger?

They are in the doorway when you enter the weapon shop, its all in one part

Do you want to make the player health infinite? I made the script that if the player in the safe zone, player won’t die(like invicible). But if you want to change to inf health, rewrite the setDeath function:

local function setDeath(character, Bool)
	local humanoid: Humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.MaxHealth = Bool and 100 or math.huge
		humanoid.Health = Bool and 100 or math.huge
	end
end

I change that because I am afraid of people would abuse the system to regain health when they are on low health by go though the door out and in.

when i try to enter the touched part by equipping a tool with debounce it gives the touch ended values after still being in the part

local function disableWeapons(player)
	local character = player.Character 
	if character then 
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid:UnequipTools() -- unequip tools first
		end
	end

	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		for _, tool in ipairs(backpack:GetChildren()) do
			tool.Parent = player.StarterGear -- then move to starter gears
		end
	end
end

i recoded the script which had the same issue but the unequip method worked thank you

1 Like