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

Why is this raycasting gun script not working?

Asked by
Tecara 0
10 years ago

So I was learning more on Ray Casting by making the gun script from the wiki. It works and damages just fine on test dummies, but when I try it with my barriers (they have health, and when they die, they disappear.) it doesn't work. Using another free model gun, it does. The barriers DO have a humanoid in them. (would fit in under hit.Parent:FindFirstChild("Humanoid") Any ideas why it wouldn't work?

local tool = script.Parent
local user

--when the tool is equipped
tool.Equipped:connect(function(mouse)
    --store the character of the person using the tool
    user = tool.Parent 

    --when the left mouse button is clicked
    mouse.Button1Down:connect(function() 
        --make and do a hit test along the ray
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)

        --do damage to any humanoids hit
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(30)
        end

        --draw the ray
        local distance = (position - tool.Handle.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright red")
        rayPart.Transparency  = 0.5
        rayPart.Anchored      = true
        rayPart.CanCollide    = false
        rayPart.TopSurface    = Enum.SurfaceType.Smooth
        rayPart.BottomSurface = Enum.SurfaceType.Smooth
        rayPart.formFactor    = Enum.FormFactor.Custom
        rayPart.Size          = Vector3.new(0.2, 0.2, distance)
        rayPart.CFrame        = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)

        --add it to debris so it disappears after 0.1 seconds
        game.Debris:AddItem(rayPart, 0.1)
    end)
end)
0
Are you sure the humanoid is called "Humanoid", and is in the same model (without sub-models) as all of the parts in the barrier? BlueTaslem 18071 — 10y
0
yeah it is called "Humanoid", and it is not in any other models inside the model. Tecara 0 — 10y
0
Did you try making the variable humanoid also equal to hit:FindFirstChild("Humaniod")? BlackJPI 2658 — 10y
0
yes, still didn't work. I think I MAY have found a solution though. I will comment again if I don't Tecara 0 — 10y

Answer this question