Hello, I’m sorry if this is an easy fix for you, but I can’t seem to understand how to fix this,
I’ve tried using :FindFirstAncestor(‘Inputs’) instead of OBJECT.Parent.Parent, yet it yeilds the same error, the WATTAGE is an IntValue stored in the Input’s parent, (the model that holds the Inputs)'s parent.
I seriously have no Idea how to fix this
function Run()
for _, OBJECT in ipairs(script.Parent:GetDescendants()) do
if OBJECT and OBJECT:IsA('BasePart') and OBJECT.Name == 'Input' then
print(OBJECT.Parent.Name)
if OBJECT and OBJECT.Parent and OBJECT.Parent.Name == 'Inputs' then
while task.wait(OBJECT.Parent.Parent:FindFirstChild('PipeNum').Value) do
local Cooldown = 2
local Wattage = OBJECT.Parent.Parent:FindFirstChild('Wattage')
local InputPart = OBJECT
local MaxHold = OBJECT:FindFirstAncestor('Inputs').Parent:FindFirstChild('Max')
local ConnectedTo = OBJECT:FindFirstChild('PipeConnectedTo')
if ConnectedTo.Value ~= nil then
local Object = ConnectedTo.Value
local OtherWattHolder = ConnectedTo.Value.Parent.Parent:WaitForChild('Wattage')
if OtherWattHolder.Value > 0 and Wattage.Value < MaxHold.Value then
Wattage.Value += math.floor(ConnectedTo.Value.Parent.Parent:WaitForChild('Wattage').Value/100)
OtherWattHolder.Value -= 1
end
if OtherWattHolder.Value <= 0 then
print('waiting')
task.wait(Cooldown)
end
end
end
end
end
end
end
script.Parent.ChildAdded:Connect(function()
Run()
end)
Line 8, but I think when I fix Line 8, the other two lines will yield an error.
local Wattage = OBJECT.Parent.Parent:FindFirstChild('Wattage')
local InputPart = OBJECT
local MaxHold = OBJECT:FindFirstAncestor('Inputs').Parent:FindFirstChild('Max')
local ConnectedTo = OBJECT:FindFirstChild('PipeConnectedTo')
These lines, most importantly, the local Wattage one.
Just add safety checks to ensure the Instance’s parent actually exists
local Parent = OBJECT.Parent
if not Parent then continue end
local Parent2 = Parent.Parent
if not Parent2 then continue end
local Wattage = Parent2:FindFirstChild('Wattage')
well your friend isn’t really helping you clean your code much
function Run()
for _, OBJECT in ipairs(script.Parent:GetDescendants()) do
if OBJECT --[[1. Object is always true if you're looping over instances]] and OBJECT:IsA('BasePart') and OBJECT.Name == 'Input' then
print(OBJECT.Parent.Name)
if OBJECT --[[1]] and OBJECT.Parent --[[2. Object.Parent is just script.Parent (always true)]] and OBJECT.Parent.Name == 'Inputs' then
while task.wait(OBJECT.Parent.Parent:FindFirstChild('PipeNum').Value) do
-- Try to avoid writing Parent (use variables)
local Cooldown = 2
local Wattage = OBJECT.Parent.Parent:FindFirstChild('Wattage') -- You're saying Wattage can be nil when you use FindFirstChild
local InputPart = OBJECT -- Rename?
local MaxHold = OBJECT:FindFirstAncestor('Inputs').Parent:FindFirstChild('Max')
local ConnectedTo = OBJECT:FindFirstChild('PipeConnectedTo')
if ConnectedTo.Value ~= nil then
local Object = ConnectedTo.Value
local OtherWattHolder = ConnectedTo.Value.Parent.Parent:WaitForChild('Wattage')
if OtherWattHolder.Value > 0 and Wattage.Value < MaxHold.Value then
Wattage.Value += math.floor(ConnectedTo.Value.Parent.Parent:WaitForChild('Wattage').Value/100)
OtherWattHolder.Value -= 1
end
if OtherWattHolder.Value <= 0 then
print('waiting')
task.wait(Cooldown)
end
end
end
end
end
end
end
script.Parent.ChildAdded:Connect(function()
Run()
end)
reduce indentation
use variables
guard clauses
don’t use ipairs/pairs
script.Parent.ChildAdded:Connect(function()
for _, Object in script.Parent:GetDescendants() do
if not Object:IsA('BasePart') or Object.Name ~= 'Input' then continue end
local ObjectHolder = Object.Parent.Parent
print(Object.Parent.Name)
local PipeNumber = ObjectHolder:FindFirstChild('PipeNum')
if not PipeNumber then --[[ Can PipeNumber be nil? ]] continue end
while task.wait(PipeNumber.Value) do
local Cooldown = 2
local Wattage = ObjectHolder.Wattage
local MaxHold = Object:FindFirstAncestor('Inputs').Parent.Max
local ConnectedTo = Object.PipeConnectedTo
if not ConnectedTo or not ConnectedTo.Value then continue end
local OtherWattHolder = ConnectedTo.Value.Parent.Parent.Wattage
if OtherWattHolder.Value > 0 and Wattage.Value < MaxHold.Value then
Wattage.Value += math.floor(Wattage.Value / 100)
OtherWattHolder.Value -= 1
end
if OtherWattHolder.Value > 0 then continue end
print('Waiting')
task.wait(Cooldown)
end
end
end)
(note i didn’t read the issue so i have no idea what’s going on)
your code is already confusing because you’re using ‘Parent’ and :FindFirstChild() a lot