How can I make my gun shoot while moving backwards?

I have been making a simple raycasting gun, but it does not shoot backwards. When I am moving forward and looking back and clicking backwards, the bullet will not form. I think this has something to do with my script, but I don’t know what.

My code:

Local script firing fire remote event:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local gunhold = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Hold)
local reload = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Reload)
local mouse = player:GetMouse()
local bulletsleft = script.Parent.Bullets
local inputservice = game:GetService("UserInputService")
local reloading = false
local equipped = false

script.Parent.Equipped:Connect(function()
	equipped = true
	gunhold:Play()
	local cloneofgui = script.Parent.PistolGui:Clone()
	cloneofgui.Parent = player.PlayerGui
	local textlabel = cloneofgui.TextLabel
	textlabel.Text = "Bullets: "..bulletsleft.Value

	bulletsleft.Changed:Connect(function()
		textlabel.Text = "Bullets: "..bulletsleft.Value
	end)
end)	

mouse.Button1Down:Connect(function()
	if equipped then
		local cloneofgui = player.PlayerGui:FindFirstChild("PistolGui")
		local textlabel = cloneofgui.TextLabel
		if bulletsleft.Value > 0 then
			script.Parent.Fire:FireServer(mouse.Hit.p)
			bulletsleft.Value = bulletsleft.Value - 1
			print(bulletsleft.Value)
		elseif bulletsleft.Value == 0 then
			textlabel.Text = "No more bullets! Press R to reload"
			inputservice.InputBegan:Connect(function(input, gameprocessedevent)
				if input.UserInputType == Enum.UserInputType.Keyboard then
					if input.KeyCode == Enum.KeyCode.R then
						if not reloading then
							reloading = true
							textlabel.Text = "Reloading..."
							reload:Play()
							wait(2)
							bulletsleft.Value = 10
							textlabel.Text = "Bullets: "..bulletsleft.Value
							reloading = false
						end
					end
				end
			end)
		end
	else
		--do nothing
	end
end)

inputservice.InputBegan:Connect(function(input, gameprocessedevent)
	if equipped then
		local cloneofgui = player.PlayerGui:FindFirstChild("PistolGui")
		local textlabel = cloneofgui.TextLabel
		if bulletsleft.Value < 10 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				if input.KeyCode == Enum.KeyCode.R then
					if not reloading then
						reloading = true
						textlabel.Text = "Reloading..."
						reload:Play()
						wait(2)
						bulletsleft.Value = 10
						textlabel.Text = "Bullets: "..bulletsleft.Value	
						reloading = false
					end
				end
			end			
		end
	end
end)	

script.Parent.Unequipped:Connect(function()
	equipped = false
	player.PlayerGui:FindFirstChild("PistolGui"):Destroy()
	gunhold:Stop()
end)

Server script handling bullets:

local canshoot = true

script.Parent.Fire.OnServerEvent:Connect(function(player, mousepos)
	
	if canshoot then 
		
		canshoot = false
		
		local raycastparams = RaycastParams.new()
		raycastparams.FilterDescendantsInstances = {player.Character}
		raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
		
		local direction = (mousepos - script.Parent.Shoot.Position).Unit*100
		local raycastresult = game.Workspace:Raycast(script.Parent.Shoot.Position, direction)
		local intersection = raycastresult and raycastresult.Position or script.Parent.Shoot.Position + direction
		local distance = (script.Parent.Shoot.Position - intersection).magnitude
		
		local bullet = game.ServerStorage.Bullet:Clone()
		bullet.Size = Vector3.new(0.1,0.1,distance)
		bullet.CFrame = CFrame.new(script.Parent.Shoot.Position, intersection)*CFrame.new(0,0,-distance/2)
		bullet.Parent = game.Workspace

		if raycastresult then
			local hitpart = raycastresult.Instance
			local model = hitpart:FindFirstAncestorOfClass("Model")

			if model then
				if model:FindFirstChild("Zombie") then
					model.Zombie:TakeDamage(20)
				end
			end
		end

		wait(0.25)
		bullet:Destroy()
		canshoot = true
	end
	
end)

What did I do wrong?

You forgot to include your raycast params, so the character is hitting itself due to server-client lag.

game.Workspace:Raycast(script.Parent.Shoot.Position, direction,raycastparams )
1 Like

i see its whitelisting the chracter’s descendants i do think you shoul be doing on the character children im not sure