Alvinblox gun script bugged (bad)

I used part of Alvinblox’s gun script (from his Youtube video) to make a bullet line for my gun. The problem is, it does not work. (It works if your standing still but not if your moving.)
The bullet does not stay with the gun and wanders off to the side when moving. It looks really bad. How do I fix this? I need the bullet to stay with the gun.

Video of the problem:
robloxapp-20210708-1914270.wmv (1.5 MB)

The script handling the bullet:

script.Parent.MainScript.Remotes.FireGun.OnServerEvent:Connect(function(player, tool, position, part)
	if player.Character:WaitForChild("Humanoid").Health <1 then
		--player is dead
	else
		--shoot!
		local distance = (tool.Handle.CFrame.Position - position).magnitude 
		if game.Workspace:FindFirstChild(player.Name .. "s Bullet") then
			game.Workspace:FindFirstChild(player.Name .. "s Bullet"):Destroy()
		end
		
		local newBullet  = Instance.new("Part")
		newBullet.BrickColor = BrickColor.new("New Yeller")
		newBullet.Name = player.Name .. "s Bullet"
		newBullet.Transparency = 0
		newBullet.Anchored = true
		newBullet.Locked = true
		newBullet.CanCollide = false
		newBullet.Size = Vector3.new(0.01, 0.01, distance)
		newBullet.Parent = workspace	
		newBullet.CFrame = CFrame.new(tool.Main.CFrame.Position, position) * CFrame.new(0, 0, (-distance / 2))
		wait(0.000000000001)
		newBullet:Destroy()
		
		if part then
			if part.Name == "Head" or part:IsA("Accessory") then
				--headshot!
				local humanoid = part.Parent:FindFirstChild("Humanoid") 
				if humanoid then
					humanoid:TakeDamage(script.Parent.MainScript.Configuration.HeadDamage.Value)
					
				end
			else
				local humanoid = part.Parent:FindFirstChild("Humanoid") 
				if humanoid then
					humanoid:TakeDamage(script.Parent.MainScript.Configuration.RegularDamage.Value)
				end
			end
		end
	end
end)

I looked in another gun and saw they used debris service for controling their beam, should I use this?

Debris service is used to delete parts in whatever seconds you want.
Such as I want to delete a part in 5 seconds, I would do this: game.Debris:AddItem(Part, 5).

1 Like

You can’t really change the position as the player moves due to the fact that doing a while true do wait() waits around 0.03 seconds average, because it destroys the bullet faster than you can correct it. I tested it myself. Your best method would be doing a bodyMover instead of just creating a bullet and positioning it, kind of like arsenal and phantom forces where bullets come out of the gun.

1 Like

Im trying to do something like the guns in Prison Life.

you cant have waits for less than 0.03 it just defaults to 0.03 if it is below it

1 Like