How do I fix this gun?

Hello everyone. I am making a (sorta) gun system for my game, problem is… actually how about I just show it:
image
Neon yellow parts are supposed to be bullets, they do spawn whenever I click, but they do not move or face the cursor.

Directory:
image
Server Script:

-- Main Variables --
local event = script.Parent.FireEvent
local gun = script.Parent
local handle = script.Parent.Handle


-- Configuration --
local BulletSpeed = 100

-- Function --
event.OnServerEvent:Connect(function(player,pos)
	print("Input received, engaging check..")
	if player.Character:FindFirstChild(gun.Name) then -- check if gun equipped
		-- Projectile --
		print("Check successful!")
		local bullet = Instance.new("Part")
		bullet.Parent = game.Workspace
		bullet.BrickColor = BrickColor.new("Bright yellow")
		bullet.Material = Enum.Material.Neon
		bullet.Size = Vector3.new(2.235, 0.59, 0.795)
		bullet.Position = handle.Position
		bullet.CFrame.LookVector = CFrame.lookAt(pos)
		print("Bullet is created.")
		-- Movement --
		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.Parent = bullet
		BodyVelocity.Velocity = Vector3.new(0,0,BulletSpeed)
		BodyVelocity.P = 100
		print("Bullet is moving.")
	else
		print("Check unsuccessful.")
		player:Kick("Check failed, security measures engaged.")
		print(player.Name.."has been kicked.")
	end
end)

Local Script:

-- Main Variables --
local tool = script.Parent.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local event = script.Parent.FireEvent

-- Function --
mouse.Button1Down:Connect(function()
	event:FireServer(mouse.Hit.Position,player)
end)

And before any of you say anything, yes, I know, BodyVelocity is deprecated, but my friend says he uses it and it works just fine. If I finally make a working gun, I will be excited for weeks on end. I appreciate any help, thanks in advance.

Your parameters are switched around.

Also please use UserInputService and not Mouse. Please.

They arent, pos goes first, but player isnt supposed to be there.

1 Like

Your server script have a lot of errors but i fixed it

-- Main Variables --
local event = game.ReplicatedStorage:WaitForChild("FireEvent")
local gun = script.Parent
local handle = script.Parent.Handle


-- Configuration --
local BulletSpeed = 100

-- Function --
event.OnServerEvent:Connect(function(player,pos)
	print("Input received, engaging check..")
	if player.Character:FindFirstChild(gun.Name) then -- check if gun equipped
		-- Projectile --
		print("Check successful!")
		local bullet = Instance.new("Part")
		bullet.Parent = game.Workspace
		bullet.BrickColor = BrickColor.new("Bright yellow")
		bullet.Material = Enum.Material.Neon
		bullet.Size = Vector3.new(0.795, 0.59, 2.235)
		bullet.Position = handle.Position
		bullet.CanCollide = false
		bullet.CFrame = CFrame.lookAt(bullet.Position ,pos)
		print("Bullet is created.")
		-- Movement --
		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.Parent = bullet
		BodyVelocity.Velocity = bullet.CFrame.LookVector * BulletSpeed
		BodyVelocity.P = 100
		print("Bullet is moving.")
	else
		print("Check unsuccessful.")
		player:Kick("Check failed, security measures engaged.")
		print(player.Name.."has been kicked.")
	end
end)

Your code is not working because of these following codes:
-You should place Event in ReplicatedStorage instead because placing in tool make the script sometimes can’t find the event to fire
-You can’t set bullet look direction by setting LookVector. Use “bullet.CFrame = CFrame.lookAt(bullet.Position, pos)” instead. (You even forgot to put the first variables)
-Body Velocity should be set by bullet.CFrame.LookVector * BulletSpeed instead of using just Vector3.new for more accurate.
-The bullet size a bit off so i changed it a bit (This shouldn’t be much of a problem but e)
-Optional: You should set bullet.CanCollide to false to prevent it acting weird (Like messing up the direction)

2 Likes

Thank you so much!! This helped me a lot.
Now all that’s left is to make the bullets damage the player and dissapear.

1 Like

I advise that you use Debris for the bullet despawning. Don’t forget to add connections aswell for the bullet damage.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.