Code working for only 1 teleporter?

Warning :warning:
This problem may be a bit long and if you don’t have time, don’t force yourself to reply!


What I have

So I have 3 teleporters (let’s call them scanners), which are supposed to do the following:

  1. Player teleports to an “arena” with some effects played in the scanner;
  2. If player’s health is under 25, the scanner “sends” an alarm;
  3. Player dies and then it gets directly teleported to the scanner, playing some effects.

All scanners are CTRL + D (duplicated), so I am not figuring out the problem!


The problem

The problem is that the code I setted for the health detection works in only one scanner.

Video of problem


Scripts

Health detection script, located in StarterCharacterScripts

(ConnectedScanner is an ObjectValue)

local ConnectedScanner = script.Parent.Scanner
local Char = script.Parent
local HRP = Char.HumanoidRootPart
local Active = false

game:GetService("RunService").Heartbeat:Connect(function()
	Active = not ConnectedScanner.Value
	if Char.Humanoid.Health <= 25 then
		if not Active then
			Active = true
			print("changed")
			ConnectedScanner.Value.Panic.Value = true
		end
	end
end)
Scanner script (without effects for shorter code)
local MODEL = script.Parent.Parent
local SCANNER = script.Parent
local PANIC_MODEL = MODEL.PanicButton
local BUTTON_LIGHT = PANIC_MODEL.WarningLight
local CLICK_DETECTOR = PANIC_MODEL.ClickHitbox.ClickDetector
local ANIMATOR = SCANNER.Energy.Animator
local OPEN_ANIM = ANIMATOR:LoadAnimation(script:WaitForChild("OpenAnim"))
local IDLE_ANIM = ANIMATOR:LoadAnimation(script:WaitForChild("IdleAnim"))
local CLOSE_ANIM = ANIMATOR:LoadAnimation(script:WaitForChild("CloseAnim"))
local TRIGGER = SCANNER.Trigger
local SCANNING_SOUND = TRIGGER:WaitForChild("Scanning")
local DOORS_SOUND = TRIGGER:WaitForChild("Doors")
local VIRT_SOUND = TRIGGER:WaitForChild("Virt")
local ALARM_SOUND = TRIGGER:WaitForChild("Alarm")
local IN_USE = false
local PLAYERS = game:GetService("Players")
local ENABLED = true
local PANIC_VALUE = SCANNER.Panic

local function Panic()
	BUTTON_LIGHT.Color = Color3.fromRGB(255,0,0)
	CLICK_DETECTOR.MaxActivationDistance = 2
	for ITERATION, GLASS_DOOR in pairs(SCANNER:GetChildren()) do
		local loweredName = GLASS_DOOR.Name:lower()
		if not string.match(loweredName, "right") and not string.match(loweredName, "left") then
			continue
		end
		GLASS_DOOR.Color = Color3.fromRGB(255,0,0)
	end
	ALARM_SOUND:Play()
end

local function Unpanic()
	BUTTON_LIGHT.Color = Color3.fromRGB(70, 50, 255)
	CLICK_DETECTOR.MaxActivationDistance = 0
	for ITERATION, GLASS_DOOR in pairs(SCANNER:GetChildren()) do
		local loweredName = GLASS_DOOR.Name:lower()
		if not string.match(loweredName, "right") and not string.match(loweredName, "left") then
			continue
		end
		GLASS_DOOR.Color = Color3.fromRGB(255, 255, 255)
		GLASS_DOOR.Material = Enum.Material.ForceField
		GLASS_DOOR.Transparency = -1000
	end
end

TRIGGER.Touched:Connect(function(PART_THAT_TOUCHED)
	if not ENABLED then return end
	if not PART_THAT_TOUCHED.Parent:FindFirstChildOfClass("Humanoid") then return end
	local DESTINATION = SCANNER.Destination
	if not DESTINATION.Value then return end
	local PLAYER = game:GetService("Players"):GetPlayerFromCharacter(PART_THAT_TOUCHED.Parent)
	local CHARACTER = PLAYER.Character
	local HUMANOID = CHARACTER.Humanoid
	local HRP = CHARACTER.HumanoidRootPart
	local VIRT_SOUND = CHARACTER:WaitForChild("VirtSound")
	local ORIGINAL_BODY_COLORS = CHARACTER["Body Colors"]
	local FAKE_BODY_COLORS = ORIGINAL_BODY_COLORS:Clone()
	FAKE_BODY_COLORS.Parent = script
	local RANDOM_VALUE = math.random(5,15)
	--this is where effects would stand
	HUMANOID.Died:Connect(function()
		if IN_USE == true then
			for ITERATION, BASEPART in pairs(CHARACTER:GetDescendants()) do
				if BASEPART:IsA("BasePart") then
					BASEPART.Material = Enum.Material.ForceField
					BASEPART.Color = Color3.fromRGB(13, 105, 172)
				end
			end
		end
	end)
	PLAYER.CharacterAdded:Connect(function(NEW_CHARACTER)
		ENABLED = false
		HUMANOID = NEW_CHARACTER.Humanoid
		HRP = NEW_CHARACTER.HumanoidRootPart
		game:GetService("RunService").Heartbeat:Wait()
		HRP.CFrame = TRIGGER.CFrame
		HRP.Orientation = TRIGGER.Orientation
		PLAYER.CameraMaxZoomDistance = 0.5
		SCANNING_SOUND:Play()
		wait(SCANNING_SOUND.TimeLength)
		OPEN_ANIM:Play()
		DOORS_SOUND:Play()
		wait(OPEN_ANIM.Length)
		PLAYER.CameraMaxZoomDistance = 25
		IDLE_ANIM:Play()
		for ITERATION, GLASS_DOOR in pairs(SCANNER:GetChildren()) do
			local loweredName = GLASS_DOOR.Name:lower()
			if not string.match(loweredName, "right") and not string.match(loweredName, "left") then
				continue
			end
			GLASS_DOOR.Material = Enum.Material.Glass
			GLASS_DOOR.Transparency = 0.35
		end
		wait(10)
		print("done")
		ENABLED = true
		IN_USE = false
	end)

	CLICK_DETECTOR.MouseClick:Connect(function()
		Unpanic()
		HUMANOID.Health = 0
	end)
	PANIC_VALUE.Changed:Connect(function()
		if PANIC_VALUE.Value == true then
			Panic()
		end
	end)
end)

Tell me if you don’t understand something! (I wrote this post like this to make everyone understand from first place)
Could anyone help me with this? If yes, thank you so much!

instead of using a renderstep for getting the health try using Humanoid.HealthChanged
https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

Thank you! I will try and reply to you if it doesn’t.

1 Like

It looks like it doesn’t work. Same problem as before, “changed” doesn’t print.
I also tried with no debounce, since it’s not repeating, but it still doesn’t work; same problem.


Fixed it

Thanks @MrchipsMa for helping.

Issue?

It looks like for some reason, the code wasn’t updating when I duplicated the scanners.

1 Like