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:

01PunchHit = script.Parent.Parent.PunchHit
02 
03function onTouched(hit)
04    while PunchHit.Value == true do
05        local human = hit.Parent:findFirstChild("Humanoid")
06        if (human ~= nil) then
07            if script.Parent.Parent.Robot.Health > 0 then
08                human.Health = human.Health - 50 -- Change the amount to change the damage.
09            end
10        end
11        PunchHit.Value = false
12    end
13 
14end
15 
16script.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:

01--local script or client
02 
03local PunchHit = script.Parent.Parent.PunchHit
04local players = game.Players:GetPlayers()
05local player = game.Players.LocalPlayer
06local part = workspace.Part -- define where the part is
07 
08local function onChange()
09    if PunchHit.Value == true then
10        for _,plr in pairs(players) do
11            if plr ~= player then
12                local distance = plr:DistanceFromCharacter(part.CFrame.Position)
13 
14                if distance <= 4.5 then -- if the player is in 4.5 studs from "part" then
15                    plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 50
View all 23 lines...

: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