Detect if the value exists or not

Hello, developers!

I am trying to detect if the value exists or not.

Script
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)

I don’t understand what you mean. Please provide more context.

Hey, it would be helpful if you included which value you wanted to detect. but this can be done universally with the following steps

value = (whatever you want to set it as)
if value ~= nil then
 -- run the code if the value is not nothing
end
local value= parent:FindFirstChild("Value name")
if value  then 
--script
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.

he wants an instance right?
find first child will return nil if the instance isnt there

Ah okay! thanks for the correction

The script won’t detect the value.

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)

Which value are you trying to detect?

The boolvalue, which is created by a script.

Your checking if the instance exists, not if the value is true or false I think. Try this:

local lighton = char:FindFirstChild("LightOn").Value
		if dist < 10 then
			if lighton == true/false then

The code you provided doesn’t work.

Did you change it to your desired value or did you leave the “true/false” as it was?

Are you recieving any output in the console?

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)
1 Like

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)