Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Regenerate Button Shouldn't Allow Clones, But Does?

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.

1 answer

✓ Accepted answer

Hello, DeanozDezignz. I will be helping you regarding the question, "Regenerate Button Shouldn't Allow Clones, But Does?"

Take a look below to see how i did it differently

Object precedence of all of my objects

ClickPart and Pad in the Workspace

TrainModel in the ReplicatedStorage

CloneScript in the ServerScriptService

Here is the Code

--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)

##Still unsure whether the script works or not?? Take a look at my video!

Accept the answer if helped and have a lovely day of coding!

2 comments

0

Thanks so much, this has helped me out a ton with my new game. Thank you also for doing a working demonstration, if I could give you extra points I would gladly do that. Best regards, DeanozDezignz.

0

No problem. I look forward on playing your game

This discussion is preserved as it was. New answers, comments, and votes are closed.