I have created a script where the platform will turn red when a vehicle is present on it. This is to prevent unwanted spamming and such. The problem is that while the platform detects the presence of a vehicle on top of it, it cannot revert back to its original color afterwards (when there isn't a vehicle on it). It is only reverted back when a non-child touches it, but I want it so that it updates automatically without such.
function onTouch(object) local Vehicle = object.Parent:FindFirstChild("VehicleSeat") if (Vehicle ~= nil) then script.Parent.BrickColor = BrickColor.Red() elseif (Vehicle == nil) then script.Parent.BrickColor = BrickColor.Gray() end end game.workspace.Part.Touched:Connect(onTouch)
Here is your problem: This code will run when something is touch it, but it won't run when something not touch it anymore. So the solution is using TouchEnded event for this. Like the name said, this event will fire when something not touch it anymore.
local function onTouch(object) local Vehicle = object.Parent:FindFirstChild("VehicleSeat") if Vehicle then script.Parent.BrickColor = BrickColor.Red() end end local function endTouch(object) local Vehicle = object.Parent:FindFirstChild("VehicleSeat") if not Vehicle then script.Parent.BrickColor = BrickColor.Gray() end end game.workspace.Part.Touched:Connect(onTouch) game.workspace.Part.TouchEnded:Connect(endTouch)
Hope this help you.