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

[Solved] Why does this if statement still happen when it should not?

Asked by
tjtorin 172
4 years ago
Edited 4 years ago

I created a spell that explodes and deals damage based on who was in the explosion using the hit event. I want the players inside the explosion to take 15 damage but instead, they take infinite damage because the if statement on line 35 always goes through even when it should not.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local testSpell = ReplicatedStorage:WaitForChild("TestSpell")
local demon_circle = ReplicatedStorage:WaitForChild("Assets").demon_circle

testSpell.OnServerEvent:Connect(function(player, hit)
    local p = demon_circle:Clone()

    p.Parent = workspace
    p:SetPrimaryPartCFrame(hit)
    p.PrimaryPart.Anchored = true
    p.PrimaryPart.Orientation = Vector3.new(0, 0, 0)

    -- Rotate
    for i = 1, 90, 1 do
        p.PrimaryPart.Orientation = Vector3.new(0, i, 0)
        wait(.000001)
    end

    -- Explosion (Visual only)
    local e = Instance.new("Explosion")
    e.Parent = workspace
    e.Position = p.PrimaryPart.Position
    e.DestroyJointRadiusPercent = 0
    e.BlastPressure = 0
    e.BlastRadius = 7.5

    hitList = {"Undefined"}

    local function damage_handler(character)
        local canDamage = false

        for i = 1, #hitList do
            wait(.0000001)
            --print(hitList[i])
            if hitList[i] ~= character.Name then
                print("Has not been hit yet")
                table.insert(hitList, #hitList+1, character.Name)
                canDamage = true
            end
        end

        if canDamage then
            character.Humanoid:TakeDamage(15)
        end
    end

    e.Hit:Connect(function(hit)
        local hum = hit.Parent:FindFirstChild("Humanoid")
        if hum then
            --[[for _, name in pairs(hitList) do
                wait(.0000001)
                print(name)
                if name ~= hit.Parent.Name then
                    table.insert(hitList, #hitList+1, hit.Parent.Name)
                    hum:TakeDamage(15)
                end
            end--]]
            damage_handler(hit.Parent)
        end
    end)

    p:Destroy()
end)
0
use a debounce... KDarren12 705 — 4y
0
debounce should work Rynappel 212 — 4y
1
^^^ a debounce for each player or else it’ll stop damaging everyone once one player is hit SmartNode 383 — 4y
1
wait(0.00000000001) is stupid inefficient. It's like waiting for zero seconds, just use game:GetService('RunService').Heartbeat:Wait() or something, or just use wait(). Fifkee 2017 — 4y

Answer this question