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

Bullet not spawning correctly when the player is moving?

Asked by 3 years ago
Edited 3 years ago

![Image from Gyazo](https://i.gyazo.com/cf8c06eb9792c1a1f389ec9652a1c9e4.gif)

Can someone give me an explanation and answer on why this is happening? My gun is a fastcast gun btw.

Client:

local context = game:GetService("ContextActionService")
local equiptimes = 0
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
local bullets = gun:WaitForChild("Bullets")
local ammo = gun:WaitForChild("Ammo")
local clipsize = 30
local animateequip = script.Parent.animationequip

mouse.Icon = "rbxassetid://117431027"

gun.Activated:Connect(function()
    if bullets.Value > 0 and shooting == false then
    shooting = true
    while shooting and bullets.Value > 0 do
        wait(0.1)
        bullets.Value = bullets.Value - 1
        player.PlayerGui.AmmoGui.Ammo.Text =  "" .. tostring(bullets.Value) .. " / " .. tostring(ammo.Value)
            gunshot:Play()
        remoteevent:FireServer(mouse.Hit.Position) 
        gun.Deactivated:Connect(function()
                shooting = false

            end)
            if bullets.Value == 0 then 
                shooting = false
                break
            end
        end
        end
    end)

server:

local RNG = Random.new()
local gun = script.Parent
local remoteevent = gun.clientserver
local fastcast = require(gun.FastCastRedux)
local caster = fastcast.new()
local Firepoint = gun.Handle.Attachment
local MIN_BULLET_SPREAD_ANGLE = 1
local MAX_BULLET_SPREAD_ANGLE = 10
local bulletsFolder = workspace:WaitForChild("BulletFolder") or Instance.new("Folder", workspace) 
bulletsFolder.Name = "BulletFolder"
local bulletTemplate = game.ServerStorage.BulletKill:Clone()
bulletTemplate.Parent = bulletsFolder
local randomdamage = math.random(10, 20)

local function onrayhit(cast, result, velocity, bullet)
    bullet:Destroy()
    local hitpart = result.Instance
    local hitpoint = result.Position
    local normal = result.Normal
    local character = hitpart:FindFirstAncestorWhichIsA("Model")
    if character and character:FindFirstChild("Humanoid") then
        character.Humanoid:TakeDamage(randomdamage)
    end
    game.Debris:AddItem(bullet, 4)

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, 0, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate

local function onequipped()
    castParams.FilterDescendantsInstances = {gun.Parent, bulletsFolder}
end


local function fire(player,mousePos)
    game.Workspace.BulletFolder.BulletKill.Attacker.Value = player.Name

    local origin = Firepoint.WorldPosition
    local direction = (mousePos - origin).Unit  
    caster:Fire(origin, direction, 500, castBehavior)

end


gun.Equipped:Connect(onequipped)

remoteevent.OnServerEvent:Connect(fire)
caster.LengthChanged:Connect(onlengthchanged)
caster.RayHit:Connect(onrayhit)




Edit: Tell me if the gyazo link doesn't work, I'll fix the link again.

Answer this question