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

Why doesn't the ray casting script work correctly?

Asked by 5 years ago

All of the scripts are correct but it won't shoot for some reason.I have tried fixing it didn't help.

Gun main:

--Made by: Hirez33
--
-- November 11 2018

--[[

    This script manages the raycasting of the gun

--]]

--Variables

local remote = game.ReplicatedStorage:WaitForChild("Remote"):WaitForChild("GunEvent")

--Main

remote.OnClientEvent:connect(function (player, barrelPos, mousePos)
    local ray = Ray.new(barrelPos,(barrelPos - mousePos).unit * 300)
    local part, position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)

    local dist = (barrelPos - mousePos).magnitude

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

    remote:FireAllClients(player, part, position, barrelPos)
    end)

Local script:

-- Made by: Hirez33
--
-- November 11 2018

-- [[

     Local script in charge of out gun ;)

]]--

-- Variables
local gun = script.Parent
local remote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")

--Main

gun.Equipped:connect(function (mouse)
    mouse.Button1Down:connect(function ()
        remote:FireServer(gun.Barrel.CFrame.p, mouse.Hit.p)
    end)

end)

BulletGenerate:

--Made by Hirez33
--
-- November 11 2018

--[[

    This script will generate the bullet for the player to see

--]]

--Variable

local remote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("GunEvent")

--Main

remote.OnClientEvent:connect(function (player, part, position, dist, barrelPos)

    local bullet = Instance.new("Part")
    bullet.Anchored = true
    bullet.CanCollide = false
    bullet.BrickColor = player.TeamColor
    bullet.Material = "Neon"
    bullet.TopSurface = "Smooth"
    bullet.BottomSurface = "Smooth"
    bullet.Size = Vector3.new(0.3,0.3,dist)
    bullet.CFrame = CFrame.new(barrelPos, position) * CFrame.new(0,0 -dist/2)

    bullet.Parent = game.Workspace
    game.Debris:AddItem(bullet,0.1)
end)
0
I believe you have to have a MouseClick script in order for it to work. fr2013 88 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

It's not working because when you subtract from the position of the barrel rather then subtracting from the mouse position, you get a position that instead would become the position of the barrel, subtracted by the mouse, meaning that it's only gonna be going from the barrel, and not in front.

Also when you fire all clients, it has to be from the server, not the client. It doesn't make sense to fire all clients when you are firing from the client its self.

Also, you want to fire the server with the correct arguments you have already.

Ad

Answer this question