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

WHY IS THERE A DISTANCE BETWEEN MY GUN AND MY BULLET WHEN FIRING?

Asked by 3 years ago
Edited by JesseSong 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Link:

https://gyazo.com/39897f0743f2fe565c9740b6965b1316 There is a distance between my gun and the bullet. I tried using GetNetwordOwner()

Note: Even If I position my firepoint it still has a distance, I even tried to position it to the very back but it didn't work.

How do I fix that? Heres my code I am currently using fastcast

Client:

local gun = script.Parent

local remoteevent = gun:WaitForChild("clientserver")

local player = game:GetService("Players").LocalPlayer


local mouse = player:GetMouse()

local gunshot = gun.ak47shotsound

local shooting = false



gun.Activated:Connect(function()
    shooting = true

    while shooting do

        wait(0.1)

            gunshot:Play()

        remoteevent:FireServer(mouse.Hit.Position)

        gun.Deactivated:Connect(function()

            shooting = false

        end)

            end

        end)

Server:

local gun = script.Parent

local remoteevent = gun.clientserver

local fastcast = require(gun.FastCastRedux)

local caster = fastcast.new()

local Firepoint = gun.Handle.Attachment


local bulletsFolder = workspace:WaitForChild("BulletFolder") or Instance.new("Folder", workspace) 

bulletsFolder.Name = "BulletFolder"

local bulletTemplate = Instance.new("Part")

bulletTemplate.Name = "Bullet"

bulletTemplate.Anchored = true

bulletTemplate.CanCollide = false

bulletTemplate.BrickColor = BrickColor.new("New Yeller")

bulletTemplate.Material = Enum.Material.Neon

bulletTemplate.Size = Vector3.new(2.846, 0.296, 0.279)

bulletTemplate.Shape = "Cylinder"




local function onrayhit(cast, result, velocity, bullet)

    local rayhit = result.Instance

    local character = rayhit:FindFirstAncestorWhichIsA("Model")

    if character and character:FindFirstChild("Humanoid") then

        character.Humanoid:TakeDamage(15)
    end

    game.Debris:AddItem(bullet, 0.1)

end

local function onlengthchanged(cast, lastpoint, direction, length, velocity, bullet)
    if bullet then

        local bulletlength = bulletTemplate.Size.Z/2

        local offset = CFrame.new(0,0,-(length - bulletlength))

        bullet.CFrame = CFrame.lookAt(lastpoint, lastpoint + direction):ToWorldSpace(offset)*CFrame.Angles(0,math.rad(-90),0)

    end


end

local castParams = RaycastParams.new()

castParams.FilterType = Enum.RaycastFilterType.Blacklist

castParams.IgnoreWater = true


local castBehavior = fastcast.newBehavior()

castBehavior.RaycastParams = castParams

castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)

castBehavior.AutoIgnoreContainer = false

castBehavior.CosmeticBulletContainer = bulletsFolder

castBehavior.CosmeticBulletTemplate = bulletTemplate


local function onequipped()

    castParams.FilterDescendantsInstances = {gun.Parent, bulletsFolder}

end


local function fire(player,mousePos)

    local origin = Firepoint.WorldPosition

    local direction = (mousePos - origin).Unit

    caster:Fire(origin, direction, 800, castBehavior)

end


gun.Equipped:Connect(onequipped)

remoteevent.OnServerEvent:Connect(fire)

caster.LengthChanged:Connect(onlengthchanged)

caster.RayHit:Connect(onrayhit)
0
It depends on where your 'FirePoint' is located. Try moving it a bit closer to the gun. Soban06 410 — 3y
0
No it still didn't work RichDiggerW189 2 — 3y

1 answer

Log in to vote
-1
Answered by 3 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

gun.Activated:Connect(function() gunshot:Play() remoteevent:FireServer(mouse.Hit.Position) end) try this

0
No it just made my gun fire endlessly RichDiggerW189 2 — 3y
Ad

Answer this question