Local script doesn't work

helloo, there’s this local script that doesn’t work, idk why. if i remove the workspace.childadded thing to find the specific workspace child it works. but i need to use that child!!

local player = game.Players.LocalPlayer
local ColorCorrection = game.Lighting.ColorCorrection
local originalColor = ColorCorrection.TintColor
local RedColor = Color3.fromRGB(255, 0, 0)

workspace.ChildAdded:Connect(function()
	wait(1)
	local children = workspace:GetChildren()

	if children:FindFirstChild("distance") then
		local npc = children
		while task.wait() do
			-- just stuff (which i verified works)
		end
	end
end)

thank youu

why gettin children without the parameter???
ChildAdded already returns the object being added in the parameters, you could’ve just use that

you’re also checkin the table which doesn’t get the children you’re trying to find, GetChildren only returns a table of the children in that case becoming a table.

local player = game.Players.LocalPlayer
local ColorCorrection = game.Lighting.ColorCorrection
local originalColor = ColorCorrection.TintColor
local RedColor = Color3.fromRGB(255, 0, 0)

workspace.ChildAdded:Connect(function(child)
	wait(1)
	--local children = workspace:GetChildren() < This only returns a table

	if child:FindFirstChild("distance") then
		local npc = child
		while task.wait() do
			-- just stuff (which i verified works)
		end
	end
end)

ohh thanks, is there any way to check often if the wanted child is in workspace? or to make the script run everytime a child enters workspace?

The above script runs everytime a child is added. Dont constantly check if the wanted child is in workspace as its performace-heavy

1 Like

You’re already connecting an event that waits to fire (in that word simply runs that each time a child is added to workspace) and run the code inside so there’s no need to make any extra lines that constantly check for it

What i meant by event is exactly this one workspace.ChildAdded:Connect(function(child)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.