Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

My punch script damages unconditionally, why?

Asked by 12 days ago

I have a punch script, everything was working well until, I added the damage script. Now whenever I touch an object (even without punching), it damages the object

My script :

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local humanoid = player.Character:FindFirstChild("Humanoid")
local animate
local hand = humanoid.Parent:WaitForChild("RightHand")

local function round(n)
    return math.floor(n + 0.5)
end

mouse.KeyDown:Connect(function(key)
    if key == "z" then
        game.ReplicatedStorage.Event.AddStrength:FireServer()
        local animation = Instance.new("Animation", player.Character)
        animation.AnimationId = "rbxassetid://5390069207"
        animate = humanoid:LoadAnimation(animation)
        animate:Play()
    end
end)
mouse.KeyDown:Connect(function(key)
    hand.Touched:Wait()
    hand.Touched:Connect(function(hit)
        if key == "z" then
            if hit.Parent.Humanoid then
                hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - round(player.leaderstat.Strength.Value / 100)
            end
        end
    end)
end)
0
You should make a damage script that activates once the user presses z and deactivates after the animation is complete. super1boom 0 — 12d
0
Ok NathanBlox_Studios 100 — 11d
0
Thanks for the tip NathanBlox_Studios 100 — 11d

1 answer

Log in to vote
1
Answered by
Gojinhan 368 Moderation Voter
12 days ago
Edited 12 days ago

You're never disconnecting the touched event, which causes it to always check for hits and damage accordingly. (this is also one of the most common memory leak :P) Make a variable for the signal and disconnect after the punch should be over.

Also, I recommend using a keybinds system along with a functions module for your weapons. This will reduce the number of scripts in your game dramatically as well as being more efficient / modular.

Also mouse.KeyDown is legacy and should be deprecated but isn't lol. I recommend using context action service for this, lemme give u an example.

ps. the parent argument of instance.new is very deprecated and should not ever be used, it's extremely slow lol

local cax = game:GetService("ContextActionService")
local weaponFunctions = require(game:GetService("ReplicatedStorage").Modules.WeaponFunc)
-- this is your weapon functions module, you return functions as the values, with the weapon name as the keys ^


cax:BindAction("UseFist', function(_,state)
    local tool = character:FindFirstChildWhichIsA("Tool")
        weaponfunctions[tool.Name](character, tool.Handle)
end, false, Enum.KeyCode.Z, Enum.UserInputType.Touch, Enum.KeyCode.ButtonR2)

And your weapon functions module:

local signal

return {

["StarterFist"] = function(char, handle)
    game.ReplicatedStorage.AddStrength:FireServer()

        animation:Play()

        signal = handle.Touched:Connect(function()
                -- do whatever 
        end)

       wait(animation.Length)

       signal:Disconnect()
end)

}

This is the basics of it but if you want to take it a step further you can have a Fist class that does this function instead of parroting the same function for every fist.

Ad

Answer this question