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

It works when using greater than or equal to, but not less than or equal to?

I'm trying to make tear gas, or smoke grenade or something, idk, but it will work when using greater than or equal to, but not less than or equal to. And I want it so when they're 5 studs are closer, they are damaged.

local function damagePlr()
	player.Character.Humanoid.Health=player.Character.Humanoid.Health-10
end	
	local mag=(player.Character:GetModelCFrame().p-toxicC.Position).magnitude
	--
	while mag<=5 do
		damagePlr()
		wait(3)
	end
	--

4 comments

0

It's probably cause when you use mag >= 5, mag is probably greater than 5, like 6 or 7. You could add a print() before the while mag <= 5 do having print(mag) or something like that to see if mag is actually <= 5

0

Based on your question, I think I know the answer.The character's position doesn't update, so it gets the characters position based on where it was first. I am not on mobile, so I csnt test it yet.

0

Based on your comment*, can't*

0

Yeah that might be the problem. Put the local mag inside a loop

1 answer

✓ Accepted answer

You only update mag once, before the loop. Each time the loop iterates, it will be using the same distance. You have to recompute the distance each iteration of the loop.


Besides that, this code will only work if the player starts in range.

If they aren't range, the loop stops immediately and will never start again.

If you want something to happen whenever a condition is met, it generally should look like this:

while true do
	local mag = (player.Character:GetModelCFrame().p - toxicC.position).magnitude
	if mag <= 5 then
		damagePlr()
	end
	wait(3)
end

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