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

Help with my Raycasting?

Asked by
neoG457 315 Moderation Voter
8 years ago

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.

0
Worst case scenario is that it might be a roblox glitch. Roblox isnt flawless :p... Maybe the raycasting just... didnt hit the npc because it glitched? just sayin'... joalars2 107 — 8y

2 answers

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

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.

0
Marry Me. neoG457 315 — 8y
1
Looking at the video again, you may want to cast the ray from the HumanoidRootPart rather than the Torso since the HumanoidRootPart doesn't rotate while animating. XAXA 1569 — 8y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

0
Thanks for the code cleanup. But I'm still having the same problem... neoG457 315 — 8y

Answer this question