Hi folks, I have been at this time and time again and can't understand how to fix what I'm doing wrong. The problem is that the button (After making it's first copy of the train from ServerStorage) continues to flash from red to green and vice versa within the given time on line 9. I know exactly what the problem is and have attempted several fixes, but neither work. The problem is there are no copies of the train to start off with in Workspace, so upon pressing the button, it makes a copy, waits until the train is 14 studs away and goes back to green to allow another copy to be made. Only when you click it the second time, it continues to flash red and green. Please have a look below:
script.Parent.ClickDetector.MouseClick:Connect(function(Duplicate) local n = game.Workspace:FindFirstChild("Train") if (n == nil or (script.Parent.Position - script.Parent.Parent.Train.DriverSeat.Position).magnitude > 14) then script.Parent.BrickColor = BrickColor.new("Crimson") local c = game.ServerStorage.Train:Clone() c.Parent = game.Workspace c:MakeJoints() while true do wait(0.5) if (n == nil or (script.Parent.Position - c.DriverSeat.Position).magnitude > 14) then script.Parent.BrickColor = BrickColor.new("Parsley green") else script.Parent.BrickColor = BrickColor.new("Crimson") end end end end)
Thank you so much for taking the time to read, and if you can help me, that would be greatly appreciated.
Object precedence of all of my objects
ClickPart and Pad in the
Workspace
TrainModel in the
ReplicatedStorage
CloneScript in the
ServerScriptService
--Declaration Section --//Game Services local Workspace = game:GetService("Workspace") local ReplicatedStorage = game:GetService("ReplicatedStorage") --//Assets local ClickPart = Workspace:WaitForChild("ClickPart") local Train = ReplicatedStorage:WaitForChild("Train") local Pad = Workspace:WaitForChild("Pad") --//Function Variables local NewTrain local Detector --Processing Section local function spawnTrain () Detector = Workspace:FindFirstChild("Train") if Detector == nil or (Pad.Position - NewTrain.Body.Position).magnitude > 14 then NewTrain = Train:Clone() NewTrain.Parent = Workspace NewTrain:MakeJoints() NewTrain.Name = "Train" else while true do if (Pad.Position - NewTrain.Body.Position).magnitude > 14 or Detector == nil then break end ClickPart.BrickColor = BrickColor.Red() wait(0.5) ClickPart.BrickColor = BrickColor.Green() wait(0.5) end ClickPart.BrickColor = BrickColor.Gray() print("Undone!") end end ClickPart.ClickDetector.MouseClick:Connect(spawnTrain)