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

Can't activate function when timer reaches a certain value?

For context this is the script I'm using It's for a boss and I've been wanting to try something like this

local Torso = script.Parent.Torso
local hum = script.Parent.Human
local timer = 0

hum.Health = 2000

local function findTarget()
	local agroDistance = 999999
	local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso")
		if human and torso and v ~= script.Parent then
			if (Torso.Position - torso.Position).magnitude < agroDistance then
				agroDistance = (Torso.Position - torso.Position).magnitude
				target = torso
			end	
		end		
	end
	return target
end	

local function runTo()
	local torso = findTarget()
	if torso ~= nil then
		hum:MoveTo(torso.Position)
	end
end

local function rocket()
	local target = findTarget()
	local missile = Instance.new("Part", game.Workspace)
	missile.Size = Vector3.new(1, 3, 1)
	missile.Name = "Missile"
	missile.Anchored = false
	missile.Position = Torso.Position
	missile.CFrame = CFrame.new(missile.Position, target.Position)
	missile.BrickColor = BrickColor.Gray()
	local homing = Instance.new("RocketPropulsion",missile)
	homing.Target = target
	homing.CartoonFactor = 0
	homing:Fire()
	missile.Touched:Connect(function(hit)
		if hit and hit.Parent and hit.Parent ~= script.Parent then
			local explode = Instance.new("Explosion", workspace)
			explode.Position = missile.Position
			missile:Destroy()
		end
	end)
end

local function spawnMinion()
	local torsoPos = Torso.Position
	local minion = game.Lighting.Minion:Clone()
	minion.Parent = workspace
end

while true do
	for i = 1, 3, 1 do
		local forceField = Instance.new("ForceField", script.Parent)
		forceField.Visible = false
		wait(4)
		rocket()
		wait(1)
		forceField:Destroy()
	end
	for i = 1, 140, 1 do
		wait(0.1)
		print(timer)
		runTo()
		timer = timer + 0.1
		if timer == 7 then
			timer = 0
			spawnMinion()
		end
	end
end

Where it doesn't work is

if timer == 7 then
			timer = 0
			spawnMinion()
		end

I have it print the Value of the Timer and it goes up but it doesn't activate the function, I don't know if its me being stupid or something extra I gotta do, any help?

1 answer

It can be possible floating point error where timer can refer to 6.999999999998 instead of what you need it to, in your case since you are increasing the number by 0.1, you can check if the timer is higher than 6.9.

if timer > 6.9 then
	timer = 0
	spawnMinion()
end

You can also convert that to string which i would do using formatting, %.1f is a formatting pattern that formats float number with 1 decimal digit.

if string.format('%.1f', timer) == '7.0' then
	timer = 0
	spawnMinion()
end

If the number would be 123.434 the formatted version would round the last 2 decimal digits and return "123.4", in case of 6.999999999997 it would be "7.0".

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