local chainstart = script.Parent.ChainStartSensor
local chainend = script.Parent.ChainEndSensor
local train1 = workspace.Train_1:GetDescendants()
local train = workspace:FindFirstChild(“Train_1”)
local chainValue = workspace.Values.ChainValue.Value
local debounce = false
local train_1 = game.Workspace.Train_1
function chainon(hit)
if hit.Parent.Name == “Train_1” then
debounce = true
print(“Train has reached start of lifthill and chainValue is now 1”)
chainValue = 1
wait (30)
debounce = false
else
print(“Nothing”)
end
end
function chainoff(hit)
if hit.Parent.Name == “Train_1” then
debounce = true
print(“Train has reached end of lifthill and chainValue is now 0”)
chainValue = 0
wait(30)
debounce = false
else
print(“nothing”)
end
end
chainstart.Touched:Connect(chainon)
chainend.Touched:Connect(chainoff)
What I want is that when a brick hits the chainstart block, it goes through the chainstart function and checks if its name is “Train_1”, but it doesn’t work. Can someone help me?
local chainstart = script.Parent.ChainStartSensor
local chainend = script.Parent.ChainEndSensor
local train1 = workspace.Train_1:GetDescendants()
local train = workspace:FindFirstChild(“Train_1”)
local chainValue = workspace.Values.ChainValue.Value
local debounce = false
local train_1 = game.Workspace.Train_1
function chainon(hit)
if hit.Parent.Name == “Train_1” then
if not debounce then
debounce = true
print(“Train has reached start of lifthill and chainValue is now 1”)
chainValue = 1
wait(30)
debounce = false
end
else
print(“Nothing”)
end
end
function chainoff(hit)
if hit.Parent.Name == “Train_1” then
if not debounce then
debounce = true
print(“Train has reached end of lifthill and chainValue is now 0”)
chainValue = 0
wait(30)
debounce = false
end
else
print(“nothing”)
end
end
chainstart.Touched:Connect(chainon)
chainend.Touched:Connect(chainoff)
Keeps printing nothing, what I am looking for is the model with the name “Train_1” to be detected if hit.
If Train_1
is a Model
pehaps some of the parts have different parents inside that model, causing the hit.Parent.Name == "Train_1"
condition to be false for some of them, instead use FindFirstAncestor(name)
which returns the ancestor if an object is a descendant of name
(else returns nil):
local ancestor = hit:FindFirstAncestor("Train_1")
if ancestor then
--code
else
print("nothing")
end
2 Likes