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

How to fix Player still getting damaged by a tool on my touch function?

Asked by 3 years ago

When I use my tool it continues dmg to whomever it touches. Uhh how do I fix this?

clientscript:

local UIS = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local CD = false

local remote = game.ReplicatedStorage.SwordDmg

local tool = script.Parent


local combo = 1

local slash = script.Parent.slash

tool.Activated:Connect(function()

    local function dmg ()
        tool.sward.Touched:Connect(function(hit)
            local enemy = hit.Parent
            local EnemyHum = enemy:FindFirstChild("Humanoid")
            if not EnemyHum then return end
            if CD == false then         
                remote:FireServer(EnemyHum, combo)
            end
        end)
    end

    local anim1 = humanoid:LoadAnimation(script:WaitForChild("anim1"))
    local anim2 = humanoid:LoadAnimation(script:WaitForChild("anim2"))
    local anim3 = humanoid:LoadAnimation(script:WaitForChild("anim3"))  

    if CD == false then
        CD = true

        if combo == 1 then
            anim1:Play()
            dmg()
            combo = 2
            slash:Play()
        elseif combo == 2 then
            anim2:Play()
            dmg()
            combo = 3
            slash:Play()
        elseif combo == 3 then
            anim3:Play()
            dmg()
            combo = 1
            slash:Play()
        end

        wait(0.3)
        CD = false
    end
end)



serverscript:

local remote = game.ReplicatedStorage.SwordDmg

remote.OnServerEvent:Connect(function(plr, EnemyHum, combo)
    print("sent")
    local PlrDB = {}

    local hasBeenHit = false

    for i,v in pairs(PlrDB) do -- go through all the players that have been hit, where 'v' is a member in the table/array/dictionary and 'i' is their number in the table. hopefully you already knew this
        if v == EnemyHum.Parent then -- check if a player that has been hit is the player the part has touched
            hasBeenHit = true -- disable
        end
    end

    if hasBeenHit == false then
        if combo == 1 then
            EnemyHum.Parent.Humanoid:TakeDamage(5)
            print(combo)
        elseif combo == 2 then
            EnemyHum.Parent.Humanoid:TakeDamage(7)
            print(combo)
        elseif combo == 3 then
            print(combo)
            EnemyHum.Parent.Humanoid:TakeDamage(10)
            EnemyHum.Parent.HumanoidRootPart.Anchored = true
            wait(0.7)
            EnemyHum.Parent.HumanoidRootPart.Anchored = false
        end
        table.insert(PlrDB,EnemyHum.Parent)-- insert to table so it can't be hit again
    end

end)
1
Function on line 20 of Client script: The event is never disconnected meaning it is continuously listening for the Touched event to be fired. When the Touched event is fired, it runs the function(s) connected. This also leaves a larger issue the more the dmg() function is called (more connections are created than needed). appxritixn 2235 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You have to check if the enemy char isn't the player's own char

local function dmg ()
        tool.sward.Touched:Connect(function(hit)
            local enemy = hit.Parent

            if enemy == char then return end

            local EnemyHum = enemy:FindFirstChild("Humanoid")
            if not EnemyHum then return end
            if CD == false then         
                remote:FireServer(EnemyHum, combo)
            end
        end)
    end
0
uhh thats not what im trying to solve, sorry i should have clarified in the post. After I use the tool once whenever It touched a player it continuously damages the humanoid even if I dont use the tool. monsterdanger16 13 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If you want to control when it can hurt the player, simply use variables. Make a bool value and put it somewhere and name it something that let's it know it belongs to the specific tool, put it IN the tool that's fine. When you want it to hurt players, turn it to true, if not, aka, when it's done, make it false. Then make the script only damage the player if they touch the object AND the value is true. I would do this and make separate scripts so they don't stack.

But you don't gotta do this.

You could, if you just want to use that function, as said, you must disconnect it afterwards, something like this:

connection = nil

Tool.activated:Connect(function()

     function name()

          connection = sword.Touched:Connect(function()end)

     end

     Blabla bla
     name()

end)

Tool.deactivated:Connect(function()

     if not (connection == nil) then -- this means the connection exists, this is so it doesn't error by trying to delete it even if it doesn't exist

connection:Disconnect() -- disconnects it, so it doesn't do whatever is inside anymore, which was a problem that causes stacking, which is running multiple of the same code unintentionally 
connection = nil

end

end)

I hope this helps :3

Answer this question