Gun not working

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a basic gun that shoots bullets
  2. What is the issue? Include screenshots / videos if possible!
    Players.S3CR3TH4CK.Backpack.Pistol.Server:17: attempt to perform arithmetic (sub) on Instance and Vector3
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Couldn’t find any related topics on the DevForum

Server Script:

local gun = script.Parent

local shootRE = script.Parent:WaitForChild("PistolShoot")
local reloadRE = script.Parent:WaitForChild("PistolReload")

local debounce = false

shootRE.OnServerEvent:Connect(function(target)
	if not debounce then
		debounce = true
		
		local projectile = Instance.new("Part")
		projectile.Size = Vector3.new(0.5,0.5,0.5)
		projectile.BrickColor = BrickColor.new("Gold")
		projectile.CFrame = script.Parent.Handle.CFrame
		
		local direction = (target - gun.Handle.Position).unit
		local distance = (target - gun.Handle.Position).Magnitude
		
		local speed = 100
		local estTime = distance/speed
		
		local velocity = direction * speed
		
		game:GetService("Debris"):AddItem(projectile, estTime)
		
		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.Velocity = velocity
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Parent = projectile
		
		script.Parent.Ammo.Value -= 1
		
		local connection = game:GetService("RunService").Stepped:Connect(function(step)
			if projectile then
				projectile.CFrame = CFrame.new(projectile.Position, projectile.Position + velocity) * CFrame.new(0,0,-velocity.magnitude * step)
			end
		end)
		
		local connection2 = projectile.Touched:Connect(function(hit)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				hum:TakeDamage(10)
			end
		end)
		
		task.wait(1.5)
		debounce = false
		task.wait(3.5)
		connection:Disconnect()
		connection = nil
		connection2:Disconnect()
		connection2 = nil
		projectile:Destroy()
	end
end)

Local Script:

local shootRE = script.Parent:WaitForChild("PistolShoot")
local reloadRE = script.Parent:WaitForChild("PistolReload")
local plr = game.Players.LocalPlayer
local hum = plr.Character:FindFirstChild("Humanoid") or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local connection

local crosshair = "rbxassetid://"

local anims = script:WaitForChild("Animations")

local hold = hum.Animator:LoadAnimation(anims.Hold)
local shoot = hum.Animator:LoadAnimation(anims.Shoot)
local reload = hum.Animator:LoadAnimation(anims.Reload)

local ammo = script.Parent.Ammo

gun.Equipped:Connect(function()

	mouse.Icon = crosshair
	
	local ammoUI = script.Ammo:Clone()
	ammoUI.Parent = plr.PlayerGui

	hold:Play()
	
	connection = gun.Activated:Connect(function()
		if ammo.Value > 0 then
			shootRE:FireServer(mouse.Hit.Position)
			shoot:Play()
		else
			reloadRE:FireServer()
			reload:Play()
		end
	end)
end)

The first argument passed for RemoteEvent.OnServerEvent() is always the player whose client fired the remote, so in your server script rewrite your function’s parameters to:

shootRE.OnServerEvent:Connect(function(player, target)

First parameter must be the player in remote events

I’ve fixed it but now it doesn’t seem to detect when the bullet touches a player

I’ve fixed it but now it doesn’t seem to detect when the bullet touches a player

1 Like

Hmm i think this line destroys the projectile before touching

I figured it out, it’s this part that breaks it

local connection = game:GetService("RunService").Stepped:Connect(function(step)
			if projectile then
				projectile.CFrame = CFrame.new(projectile.Position, projectile.Position + velocity) * CFrame.new(0,0,-velocity.magnitude * step)
			end
		end)
		```

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