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

Why is a specific code not cloning another LocalScript into the hit.Parent(Person who touched)?

I have a touched script that does codes, After the codes I put a wait time and then a clone code which clones and puts a LocalScript into the same person who touched the Part, It doesn't even clone, The rest of the code works, Here is the whole code:


local ATMPart = script.Parent

wait ()

ATMPart.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Torso = hit.Parent.Torso
    local ATM = game.Workspace.ATM
    if player and hit.Parent:FindFirstChild("Card") then
	
	wait ()
	hit.Parent.Humanoid.WalkSpeed = 0
	wait (0.5)
	
	Torso.CFrame = CFrame.new(Torso.Position, ATM.Position)
	
	wait (0.2)
	
    hit.Parent.Humanoid.WalkSpeed = 10
	
	while wait() do
		local ATM = game.Workspace.ATMLine.Position
	hit.Parent.Humanoid:MoveTo(ATM)
	 
	end
	
	
		hit.Parent.Humanoid.WalkSpeed = 0
		
		wait (0.1)
		
		local new_script = script:findFirstChild("LocalScript"):Clone()
new_script.Parent = hit.Parent
		
	end
     end)

Here is the part that isn't working atm

local new_script = script:findFirstChild("LocalScript"):Clone()
new_script.Parent = hit.Parent

Why is it not cloning!?

No errors in the output either.

1 answer

✓ Accepted answer
--tab your code right >:(

while wait() do
	local ATM = game.Workspace.ATMLine.Position
	hit.Parent.Humanoid:MoveTo(ATM)
end

hit.Parent.Humanoid.WalkSpeed = 0

local new_script = script:findFirstChild("LocalScript"):Clone()
new_script.Parent = hit.Parent

That loop is infinite. If a loop is infinite, obviously any code after it will never run. That's why the script doesn't clone.

The solution depends on your exact situation, but one easy thing would be to simply put the infinite loop last.

local new_script = script:findFirstChild("LocalScript"):Clone()
new_script.Parent = hit.Parent

hit.Parent.Humanoid.WalkSpeed = 0

while wait() do
	local ATM = game.Workspace.ATMLine.Position
	hit.Parent.Humanoid:MoveTo(ATM)
end

Also, you're almost definitely going to want to add a debounce.

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