local Players = game:GetService("Players")
local Part = script.Parent
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
local rootpart = char:FindFirstChild("HumanoidRootPart")
local dist = (Part.Position - rootpart.Position).Magnitude
if dist < 75 then
if char:WaitForChild("LightOn") then
if humanoid.Health > 0 then
task.wait(0.5)
humanoid:TakeDamage(1)
end
end
end
end
end)
find first child would return an error if the value wasn’t there, “index nil with find first child”, no?
Also i’m 70% sure they want something numeric, correct me if i’m wrong though.
local Players = game:GetService("Players")
local Part = script.Parent
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
local rootpart = char:FindFirstChild("HumanoidRootPart")
local dist = (Part.Position - rootpart.Position).Magnitude
local lighton = char:FindFirstChild("LightOn")
if dist < 10 then
if lighton ~= nil then
if humanoid.Health > 0 then
task.wait(0.5)
humanoid:TakeDamage(1)
end
end
end
end
end)
local Players = game:GetService("Players")
local Part = script.Parent
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
local rootpart = char:FindFirstChild("HumanoidRootPart")
local dist = (Part.Position - rootpart.Position).Magnitude
local lighton = char:FindFirstChild("LightOn")
if dist < 75 then
if lighton then
if lighton.Value then
if humanoid.Health > 0 then
task.wait(0.5)
humanoid:TakeDamage(1)
end
end
end
end
end
end)
Do not use WaitForChild inside a Heartbeat connection, this yields code and is unnecessary considering the code is executed very often(multiple times within a second, in a loop-like manner). Also it may cause a pointless infinite yield, in general if you don’t plan to disconnect a connection afterwards, it’s good to avoid such yields. Here’s what you can do instead:
local RunService = game:GetService("RunService")
local Part = script.Parent
local function damagePlayer(player: Player): ()
local c = plr.Character
if not c then return end
local humanoid = c:FindFirstChildOfClass("Humanoid")
local root = c:FindFirstChild("HumanoidRootPart")
if not root then return end
local dist = (Part.Position - root.Position).Magnitude
if dist >= 75 then return end
local val = c:FindFirstChild("LightOn")
if not val then return end --perhaps if not val or not val.Value?
if not humanoid or humanoid.Health <= 0 then return end
task.wait(0.5)
humanoid:TakeDamage(1)
end
RunService.Heartbeat:Connect(function()
for _, player in pairs(game.Players:GetPlayers()) do
damagePlayer(player)
end
end)
Checking bool value isn’t the same as checking a normal bool datatypes, you’re checking if it has the value, not if it’s true or false
local Players = game:GetService("Players")
local Part = script.Parent
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
for i, plr in ipairs(Players:GetPlayers()) do
local char = plr.Character
local humanoid = char:FindFirstChildOfClass("Humanoid")
local rootpart = char:FindFirstChild("HumanoidRootPart")
local dist = (Part.Position - rootpart.Position).Magnitude
local lighton = char:FindFirstChild("LightOn")
if dist < 75 then
if lighton and lighton.Value == true then --if there's lighton and and lighton is true or false
if humanoid.Health > 0 then
task.wait(0.5)
humanoid:TakeDamage(1)
end
end
end
end
end)