I was told to replace my hitbox with Raycasting so I made this script that inflicts damage each time I click. Sometimes it works and other times it doesnt and I have no idea why.
This is what it looks like: Punch
You can see that even though a punch was thrown it didnt inflict damage like the other ones.
Heres the script:
ID1 = "http://www.roblox.com/Asset?ID=343805145" ID2 = "http://www.roblox.com/Asset?ID=344655490" ID3 = "http://www.roblox.com/Asset?ID=344667896" AttackList = {ID1, ID2, ID3,} local Player = game.Players.LocalPlayer repeat wait() until game.Players.LocalPlayer.Character local Mouse = Player:GetMouse() local new = 1 enabled = true Mouse.Button1Down:connect(function(key) if not enabled then return end enabled = false local TestAttack = Instance.new("Animation") TestAttack.AnimationId = AttackList[new] local animTrack = Player.Character.Humanoid:LoadAnimation(TestAttack) animTrack:Play() local ray = Ray.new(Player.Character.Torso.Position, Vector3.new(0, 0, 10)) local part, endPoint = workspace:FindPartOnRay(ray) if part == nil then else if part ~= nil then local human = part.Parent:FindFirstChild("Humanoid") if human and (not debounce) and (human.Parent ~= Player.Character) then debounce = true end end end wait(0.3) enabled = true end)
I think the problem is at lines 22-29 but I don't know what they are. Please help.
I'm assuming you want a cast a ray that shoots directly out of the front of the torso and is 10 studs long. If so, replace line 22 (local ray = Ray.new(Player.Character.Torso.Position, Vector3.new(0, 0, 10))
) with this:
local PUNCH_RANGE = 10 local torso = Player.Character.Torso local ray = Ray.new(torso.Position, torso.CFrame.lookVector * PUNCH_RANGE)
Explanation
From the wiki page on Ray: A Ray is a half-line, that is finite in one direction, but infinite in the other. It can be defined by a 3D point, where the line originates from, and a direction vector, which is the direction it goes in. The direction is similar to the lookVector of CFrame.
This particular snippet of code, torso.CFrame.lookVector * PUNCH_RANGE
, gets the unit vector (a one stud long line) the torso is facing and multiplies it by PUNCH_RANGE (10) studs to make the final direction vector.
Do this in conjunction with BlueTaslem's advice.
First, code cleanup.
Tab your code correctly.
There's no point to an empty if
like on line 25. Further, there's no point in an if
inside an else
. You can use elseif
. But even that's not necessary since you're just saying part ~= nil
which is the opposite of part == nil
.
if part ~= nil then local human = part.Parent:FindFirstChild("Humanoid") if human and (not debounce) and (human.Parent ~= Player.Character) then debounce = true; end end
Instead of human.Parent ~= Player.Character
, you should just ignore that in the RayCast:
local part, endPoint = workspace:FindPartOnRay(ray, Player.Character)
You only use debounce
in two places: one to check it, and one to turn it on.
You never undo debounce
, so you can only deal damage once.