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

Why doesn't my script run after this while loop is not true any more?

I have a script that when an object, it will find the nearest enemy and shoot it, but when i try to place it, it doesn't run. the while loop is meant to hold up the script until it is placed, but after i place it, there is no farther output.

Script:

local me = script.Parent
local TS = game:GetService("TweenService")
while script.Enabled.Value == false do
	wait()
	print("Waiting!")
	if script.Enabled.Value == true then
		break
	end
end

print("I've been enabled!")
--      [VARIBLES]
local attackTime = 2 -- attacks every 5 seconds
local attackDmg = 2 -- attacks do 5 damage
local attackrange = 30 -- 40 stud attack range
function rotateYAxisToFace(originalPos, lookAtPos) return CFrame.new(originalPos, Vector3.new(lookAtPos.X, originalPos.Y, lookAtPos.z)) end
print("reeeeee")
function checkParts() 
	local close =  math.huge
	local  name
for _,v in pairs(game.Workspace:GetChildren()) do
	print("checked:"..v.Name)
	if v.Name == "Example Enemie" then
		print(v.Name)
		local dis = (me.Position - v.Position).magnitude
		if dis < close then
			close = dis
			name = v
		end
	end
end
return name
end
	



function shoot(object)
	--shoot
	print("shot!")
	local arrow = Instance.new("Part", me)
	arrow.Anchored = true
	arrow.Position = me.Position + Vector3.new(0,0,-1)
	arrow.CanCollide = true
	local goal = {}
	goal.Position = object.Position
	arrow.CFrame = CFrame.new(arrow.Position,object.position) 
	local TI = TweenInfo.new(0.1, Enum.EasingStyle.Linear)
	local tween = TS:Create(arrow,TI,goal)
	tween:play()
	wait(0.1)
	arrow:Destroy()
	object.Health.Value = object.Health.Value - attackDmg
end
while true do
	local en = checkParts()
	me.CFrame = rotateYAxisToFace(me.Position,en.position)
	local distance = (en.Position - me.Position).magnitude
	if distance <= attackrange then
		shoot(en)
	end
	wait(attackTime)
end 

2 comments

0

because you need to break it

0

I did break it..?

2 answers

This is because while the script is disabled, nothing is running. As it says, it's disabled. No compiling, no reading of lines and no execution.

This being said, when it's enabled, it starts to run.

tdlr; you don't need the loop.

this part:

while script.Enabled.Value == false
	wait()
	print("Waiting!")
	if script.Enabled.Value == true then
		break
	end
end

1 comment

0

I tried it and it still doesn't point towards the enemy, buy if i have it put in a spot in the editor, and then run it it works, so if its disabled, it doesn't point towards the enemy, and i changed it to a boolvalue. so i don't know what else to do.

inside the while true do loop you need to make a statement like this

if this == true then--this is just any statement
	break--this is how you break a while loop
end

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