Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

It depends on where your 'FirePoint' is located. Try moving it a bit closer to the gun.

0

No it still didn't work

1 answer

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

1 comment

0

No it just made my gun fire endlessly

This discussion is preserved as it was. New answers, comments, and votes are closed.