Confused with if statement

local station = script.Parent

local train1 = workspace.Train_1:GetDescendants()

local train = workspace:FindFirstChild(“Train_1”)

local debounce = false

local train_1 = game.Workspace.Train_1

local stationValue = 0
local occupiedValue = station.occupiedValue.Value

function stationHit(hit)
local ancestor = hit:FindFirstAncestor(“Train_1”)
if ancestor then
if not debounce then
debounce = true
stationValue = 1
print(“Train1 has been stopped at exit”)
script.Parent.CanCollide = true – brick collide is on-- train is anchored
for _,v in pairs(train1) do
if v:IsA(“BasePart”) then
v.Anchored = true
end
print(“Train1 has been anchored at exit”)
wait(10)
debounce = false
end
elseif occupiedValue == 1 and stationValue == 1 then
debounce = true
print(“Trai1 has departed from the exit”)
script.Parent.CanCollide = false
for u,v in pairs (train1) do
if v:IsA(“BasePart”) then
v.Anchored = false
end
wait(10)
debounce = false
end
end
else
print(“Train_1 isn’t in station”)
end
end

station.Touched:Connect(stationHit)
What I want this script to do is to check if theres a model called “Train_1” touching it and if the value of a bool value called occupiedvalue is 1 as well as statinvalue is 1 then the part is uncolided and the train is unachored. However, while the first part of the code works, the part where it checks the occupiedvalue and station value doesn’t work. Can someone please help me

Assuming the value changes after this part of the script will run, it will stay as whatever value it was at when checked. Instead of getting the value here, get the value when you check it’s value right here.

local station = script.Parent

local train1 = workspace.Train_1:GetDescendants()

local train = workspace:FindFirstChild("Train_1")

local debounce = false

local train_1 = game.Workspace.Train_1

local stationValue = 0
local occupiedValue = station.occupiedValue

function stationHit(hit)
local ancestor = hit:FindFirstAncestor("Train_1")
if ancestor then
if not debounce then
debounce = true
stationValue = 1
print("Train1 has been stopped at exit")
script.Parent.CanCollide = true --brick collide is on  train is anchored
for _,v in pairs(train1) do
if v:IsA("BasePart") then
v.Anchored = true
end
print("Train1 has been anchored at exit")
wait(10)
debounce = false
end
elseif occupiedValue.Value == 1 and stationValue == 1 then
debounce = true
print("Trai1 has departed from the exit")
script.Parent.CanCollide = false
for u,v in pairs (train1) do
if v:IsA("BasePart") then
v.Anchored = false
end
wait(10)
debounce = false
end
end
else
print("Train_1 isn’t in station")
end
end

Doesn’t work, the part is on non-collide when the value is 0

Is the occupied value actually 1? Since it seems like the occupied value is just set to whatever the value is in the model but there’s no actual changes being done to it in the script. Since you’re using

The and would require both values to be true to actually run, otherwise it would just skip over it.

The value is set to 0 automatically.

Also, the occupiedValue is controlled by a button.