I have a train game, but whenever the part touches another part, the touch event does not fire. Here is the script. The part is also being tweened It is clearly touching the Promenade Part. It used to work. The seat is welded to engine, which is being tweened so the seat follows it.
local seat = script.Parent --front train script
local engine = seat.Parent..Engine
local TweenService = game:GetService("TweenService")
local TweenMovingInfo = TweenInfo.new(
250, --Seconds
Enum.EasingStyle.Linear, --Easing style
Enum.EasingDirection.In,
0, --Repeat
false, --Reversed
0
)
local Target =
{
CFrame = CFrame.new(-8440.991, 1.917, 139.211) * engine.CFrame.Rotation;
}
local startTween = TweenService:Create(engine,TweenMovingInfo, Target)
startTween:Play()
while true do
wait()
engine.Anchored = true
end
seat.Touched:connect(function(child)
if (child.Name == "Promenade") then --if touch x sensor play sound
engine.Promenade:play()
end
end)
I’m assuming the train or something else is anchored, and because of this, .Touched() events are not very accurate. The best way to fix this is by doing a neat trick:
Instead of doing
seat.Touched:connect(function(child)
if (child.Name == "Promenade") then --if touch x sensor play sound
engine.Promenade:play()
end
end)
Use
while wait(.1) do
local connection = seat.Touched:Connect(function() end)
local TouchingParts = seat:GetTouchingParts()
connection:Disconnect()
for i,v in pairs(TouchingParts) do
if v.Name == "Promenade" then
engine.Promenade:Play()
end
end
end
if needed you can add a wait(3) or however long in there after the :Play() part so it doesn’t repeatedly play either, depending on whatever you want
local seat = script.Parent --caraige
local carriage = seat.Parent
local touchseat = game.Workspace.Train.TrainCarraige1.VehicleSeat --if front of train touch sensor do it in sync
local dl = carriage.DL:GetChildren()
local dr = carriage.DR:GetChildren()
local engine = carriage.Engine
local engineBV = engine.BodyVelocity
local panels = carriage.Destination:GetChildren()
engine.BodyVelocity.MaxForce = Vector3.new(1,1,1)
local lastStation = nil
local dir = seat.Throttle
function SetDirection(_Dir)
if (_Dir == 0) then return end
dir = _Dir
for i,v in pairs(carriage.Lights:GetChildren()) do
if (v.Name == "A") then
v.SpotLight.Color = (dir > 0) and Color3.new(1, 0.95, 0.88) or Color3.new(0.66, 0, 0)
else
v.SpotLight.Color = (dir < 0) and Color3.new(1, 0.95, 0.88) or Color3.new(0.66, 0, 0)
end
end
end
while wait() do
local connection = touchseat.Touched:Connect(function() end)
local TouchingParts = touchseat:GetTouchingParts()
connection:Disconnect()
for i,v in pairs(TouchingParts) do
if v.Name == "Promenade" then
engine.Promenade:Play()
end
if v.Name == "Esplanade" then
engine.Esplanade:Play()
end
if v.Name == "RaintreeCove" then
engine.RaintreeCove:Play()
end
if v.Name == "OutramPark" then
engine.OutramPark:Play()
end
if v.Name == "Oceanfront" then
engine.Oceanfront:Play()
end
if v.Name == "AviationPark" then
engine.AviationPark:Play()
end
--Modifiers
if v.Name == "AccelerateSound" then
engine.Clattering.PlaybackSpeed = 0
repeat
wait(0.1)
engine.Clattering.PlaybackSpeed = engine.Clattering.PlaybackSpeed + 0.01
until engine.Clattering.PlaybackSpeed > 0.75
end
if v.Name == "DecelerateSound" then
engine.Clattering.PlaybackSpeed = 0.75
repeat
wait(0.1)
engine.Clattering.PlaybackSpeed = engine.Clattering.PlaybackSpeed - 0.01
until engine.Clattering.PlaybackSpeed < 0.02
end
--Station Sensor and Speed
if v.Name == "StationSensor" and v ~= lastStation then
lastStation = v
engine.Brake:Play() --play train stop sound
--print("Train stopped")
seat.Throttle = 0
SetDirection(v.DepartDirection.Value)
wait(5)
engine.DOSlide:play()
for i,v in pairs(panels) do
if (v.SetName.Value ~= "") then
v.SGUI.LineName.T.Text = v.SetName.Value
end
end
local doors = v.DoorLeft.Value and dl or dr
for i,v in pairs(doors) do
v.CanCollide = false
v.Transparency = 1
end
wait(v.WaitingTime.Value)
engine.DCAnnouncement:play() --door closing announcement
wait(4.7)
for i,v in pairs(doors) do
v.CanCollide = true
v.Transparency = 0
end
wait(6)
seat.Throttle = dir
end
if v.Name == "SpeedSensor" then
seat.MaxSpeed = v.Speed.Value
end
end
end
SetDirection(dir)
engine.Clattering:Play()
while wait(0.1) do
engineBV.velocity = engine.CFrame.lookVector * seat.Throttle * seat.MaxSpeed
engine.Clattering.Volume = engine.Velocity.Magnitude / seat.MaxSpeed
end
Take a look at my whole script. Can you please check it for anything that is affecting the touch to not work
I just discovered another thing. When the part (anchored) which is what the seat is welded to stop tweening, it does fire. The tweened part is anchored while the seat isnt but is welded. How does the tween affect touch???