Why is this code crashing Studio?

I’m making a teleporter that teleports you to a random place but when the math.random part gets the same value twice when using the teleporter it crashes? why does it do this?

local part = script.Parent

local running

local numberval

local random

part.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit.Parent.Can_Teleport.Value == true then
		hit.Parent.Can_Teleport.Value = false
		
		running = true
		
		if hit.Parent.All_Places.Value == false then
			hit.Parent.All_Places.Value = true
			for i = 1,3, 1 do
				numberval = Instance.new("IntValue")
				numberval.Parent = hit.Parent
				numberval.Value = i
				numberval.Name = i
			end
		end
		
		random = math.random(1,3)
		print(random)
		
		while running == true do
			if random == 1 then
				for i,v in pairs(hit.Parent:GetChildren()) do
					if v.ClassName == "IntValue" then
						if v.Value == 1 then
							print("I EXIST")
							running = false
							v:Destroy()
							hit.Parent:MoveTo(Vector3.new(-128.5, 25.5, 10))
						else
							
						end
					end
				end
			end
			if random == 2 then
				for i,v in pairs(hit.Parent:GetChildren()) do
					if v.ClassName == "IntValue" then
						if v.Value == 2 then
							print("I EXIST")
							running = false
							v:Destroy()
							hit.Parent:MoveTo(Vector3.new(-214.5, 17.5, 10))
						else
							
						end
					end
				end
			end
			if random == 3 then	
				for i,v in pairs(hit.Parent:GetChildren()) do
					if v.ClassName == "IntValue" then
						if v.Value == 3 then
							print("I EXIST")
							running = false
							v:Destroy()
							hit.Parent:MoveTo(Vector3.new(-46.5, 25.5, 10))
						else
							
						end
					end
				end
			end
		end
		wait(2)
		hit.Parent.Can_Teleport.Value = true
	end
end)

I think it’s because of the while true

You don’t have any sort of yield in your while loop. Add a wait() in there somewhere, or some type of yeild.

1 Like