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

How do I improve the hitbox for a punch script?

Asked by 6 years ago

so I made a script that makes an NPC punch when it gets close to the player & it is meant to damage them & I register the hit by testing for a collision with the NPC's hand but the problem is it doesn't always register & they barely do any damage. So is there any way to use CFrame or something to detect if the target is directly in front of the NPC?

here is the original script:

PunchHit = script.Parent.Parent.PunchHit

function onTouched(hit)
    while PunchHit.Value == true do
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
            if script.Parent.Parent.Robot.Health > 0 then
                human.Health = human.Health - 50 -- Change the amount to change the damage.
            end
        end
        PunchHit.Value = false
    end

end

script.Parent.Touched:connect(onTouched)

BTW 'PunchHit' is a boolean value that turns to true when the NPC plays the punch animation.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Other ways

you can use magnitude to detect if a player is in range to punch. though theres a function called player:DistanceFromCharacter(vector3 value), this will make the magnitude for you so then you can determine if the target is in a certain range or not.

Better methods and deprecated stuff

:connect() is deprecated use :Connect() instead. also you should use local variables instead of global ones as local variables are faster and you'd rarely need to use global variables anyways. :findFirstChild() is deprecated so use :FindFirstChild(); deprecation. use the :GetPropertyChangedSignal() function over while loops to reduce lag; :GetPropertyChangedSignal() is pretty much the same thing as Changed, though it listens if a certain property changes.

an example:

--local script or client

local PunchHit = script.Parent.Parent.PunchHit
local players = game.Players:GetPlayers()
local player = game.Players.LocalPlayer
local part = workspace.Part -- define where the part is 

local function onChange()
    if PunchHit.Value == true then
        for _,plr in pairs(players) do
            if plr ~= player then
                local distance = plr:DistanceFromCharacter(part.CFrame.Position)

                if distance <= 4.5 then -- if the player is in 4.5 studs from "part" then
                    plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 50
                    break -- breaks the loop
                end
            end
        end
    end
end

PunchHit:GetPropertyChangedSignal("Value"):Connect(onChange)

:DistanceFromCharacter() gets the distance between the player's head and the vector3 value if ur wondering. LocalScripts only run in certain places such as inside the player's character, StarterGui, StarterPlayer, ReplicatedFirst and StarterPack.

to do this on the server, use the player parameter to get the player instead of LocalPlayer
Ad

Answer this question