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

[SOLVED] Why is my raycasting gun not working like it's supposed to?

Asked by 3 years ago
Edited 3 years ago

Hey there! So I made a gun using a viewmodel and attaching it to the camera. All of this works fine, but the part that is not working is the shooting. I'm trying to create a raycast, but after a couple of shots the gun just hits the baseplate and not the part I'm shooting at. If I shoot even more it just prints nothing.

Shooting part of the local script (Don't worry about the "Spread" part, this is unused for now.):

mouse.Button1Down:Connect(function()
                if using and canShoot and not reloading and ammo > 0 and fp then
                    canShoot = false

                    local shootAnim = gunHumanoid:LoadAnimation(script:WaitForChild("Shoot"..currentEquipped.Name))
                    shootAnim:Play()

                    local spread = 0

                    ammo = ammo - 1

                    if aiming then
                        spread = 0
                    elseif running then
                        spread = 5
                    elseif humanoid:GetState() == Enum.HumanoidStateType.Jumping or humanoid:GetState() == Enum.HumanoidStateType.FallingDown or humanoid:GetState() == Enum.HumanoidStateType.Freefall then
                        spread = 10
                    end

                    local spreadX, spreadY, spreadZ = math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread)

                    mouse.TargetFilter = currentEquipped

                    print(mouse.Target)

                    game.ReplicatedStorage:WaitForChild("RaycastEvent"):FireServer(currentEquipped:WaitForChild("Flame").Position, mouse.Hit.Position, math.random(20,25), Vector3.new(spreadX/10, spreadY/10, spreadZ/10))

                    wait(shootAnim.Length)

                    canShoot = true
                elseif ammo <= 0 and not reloading and using then
                    Reload()
                end
            end)

Server script:

game.ReplicatedStorage:WaitForChild("RaycastEvent").OnServerEvent:Connect(function(player, fromP, toP, damage, spread)
    local raycast = Ray.new(fromP, (toP - fromP).unit * 100)
    local part, position, normal = workspace:FindPartOnRay(raycast, player.Character, false, true)
    local character = player.Character or player.CharacterAdded:Wait()
    local hitType = "Else"

    if part then
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        print(part:GetFullName())

        if humanoid then
            humanoid:TakeDamage(damage)
        end

        local parInst = Instance.new("Part", part)
        parInst.Position = toP
        parInst.Material = Enum.Material.Neon
        parInst.Size = Vector3.new(.2,.2,.2)
        parInst.Anchored = true
        parInst.CanCollide = false
        parInst.Archivable = false
    end
end)

Any help is appreciated!

2 answers

Log in to vote
4
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

Note:

WorldRoot:FindPartOnRay() has been deprecated. "Use WorldRoot:Raycast() along with RaycastParams for new work." - Roblox API

The syntax for WorldRoot:RayCast() is:

workspace:Raycast(Origin, Direction, RaycastParams)

After I converted your code to the new way of Raycasting, it worked fine for me:

game.ReplicatedStorage:WaitForChild("RaycastEvent").OnServerEvent:Connect(function(player, fromP, toP, damage, spread)
    local raycastResults = workspace:Raycast(fromP, (toP - fromP).unit * 100, RaycastParams.new())
    --local raycast = Ray.new(fromP, (toP - fromP).unit * 100)
    --local part, position, normal = workspace:FindPartOnRay(raycast, player.Character, false, true)
    local character = player.Character or player.CharacterAdded:Wait()
    local hitType = "Else"

    --if part then
    if raycastResults then
        local part = raycastResults.Instance
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        print(part:GetFullName())

        if humanoid then
            humanoid:TakeDamage(damage)
        end

        local parInst = Instance.new("Part", part)
        parInst.Position = toP
        parInst.Material = Enum.Material.Neon
        parInst.Size = Vector3.new(.2,.2,.2)
        parInst.Anchored = true
        parInst.CanCollide = false
        parInst.Archivable = false
    end
end)
0
It unfortunately didn't fix my problem, but this was helpful anyway, thanks! ColdFoxy07 76 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

I solved it! All I did was put the gun into a tool, tweaked it a little, and it worked.

Answer this question