Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 8 years ago

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
    --
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 NinjoOnline 1146 — 8y
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. SimplyRekt 413 — 8y
0
Based on your comment*, can't* SimplyRekt 413 — 8y
0
Yeah that might be the problem. Put the local mag inside a loop NinjoOnline 1146 — 8y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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
Ad

Answer this question