Raycast Gun Won't Register

  1. What do you want to achieve?

Hello! I want to make a gun with raycasting that shoots from a top down-view.

  1. What is the issue?

My issue is that everytime I fire the gun, the raycast is acting weird. It isn’t going towards the mouse, instead it appears to be hitting things not even close to the mouse sometimes.

First Video

Second Video

Output from the play session I did in the two videos.

Sorry if these videos don’t show much, but basically you can see that I am clicking on the dummy and the remote event is fully firing, sometimes it hits, sometimes it doesn’t, it’s very weird.

  1. What solutions have you tried so far?

The solutions I have tried so far are making a new variable named DIRECTION. The variable basically stored the Mouse position, but set the Y axis to the player’s humanoidrootpart Y axis. I replaced Mousehit in the script with the variable direction so that I could have the X and Z axis of the mouse, but keep the Y axis of the player. In theory I thought that could make a straight line to the mouse, but it didn’t change anything, the gun was still acting weird.

local Weapon = script.Parent


local Shoot = Weapon.Shoot



Shoot.OnServerEvent:Connect(function(Player,mousehit)
	local Character = Player.Character
	local Ammo = Weapon.Ammo
	local AmmoReserves = Weapon.AmmoReserves
	local MaxAmmo = Weapon.MaxAmmo
	
	--- check ammo capacity and stuf
	
	
	--- Fire -----------------------------------------------------------------------------------------------------------------------------------------------
	if Ammo.Value > 0 then
		Ammo.Value = Ammo.Value - 1
		
		if Ammo.Value == 0 then
			Weapon.ReloadText:FireClient(Player)
		end
		
		local Bullet = Instance.new("Part")
		Bullet.BrickColor = BrickColor.new("New Yeller")
		Bullet.Material = Enum.Material.Neon
		Bullet.CanCollide = false
		Bullet.Parent = game.Workspace
		Bullet.Anchored = true
		Bullet.CFrame = Character.HumanoidRootPart.CFrame
		Bullet.Size = Vector3.new(0.1,0.1,10)
		
		Bullet.CFrame = CFrame.new(mousehit,Character.HumanoidRootPart.Position)
		
		local bd = Character.HumanoidRootPart.Position - Bullet.Position
		
		Bullet.Size = Vector3.new(0.1,0.1,math.abs(bd.Z))
		
		local gunparams = RaycastParams.new()
		gunparams.FilterDescendantsInstances = {Character,Bullet}
		gunparams.FilterType = Enum.RaycastFilterType.Blacklist
		

		local RaycastResult = game.Workspace:Raycast(Weapon.Position,(mousehit),gunparams)
		
		if RaycastResult then
			local HitPart = RaycastResult.Instance
			print(HitPart)
			
			local Humanoid = HitPart.Parent:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				
				Humanoid:TakeDamage(30)
				
			end
		end
		wait(0.1)
		Bullet:Destroy()
	end
	--- Fire -----------------------------------------------------------------------------------------------------------------------------------------------

	
	if Ammo.Value == 0 and AmmoReserves.Value > 0 then
		
		Weapon.ReloadText:FireClient(Player)
		
	end
	
	if Ammo.Value == 0 and AmmoReserves.Value == 0 then

		Weapon.OutOfAmmo:FireClient(Player)
		
	end
	
end)

This is the code that fires the raycast. The raycast fires from a pistol welded onto the right arm of the player (Weapon variable.) The direction is mousehit which in the local script that fires the remote event named shoot is Mouse.Hit.p, or the player’s mouse pretty much.

What could I be doing wrong?

I’ve dealt with something like this before. It collides with a part on a character but doesn’t register. Print something after takeDamage. If it prints something, try random variations of the code until roblox lets it go through.

Now it just prints debug anytime it hits the enemy. Not really sure what to do with this.

image

Well, you should be assured your script is working. Try variations, such as manually subtracting the health. Humanoid.Health -= 30

well that isn’t working either, changing the way the enemy takes damage doesn’t appear to do anything, i’m not having a problem with damaging the enemy, i’m having a problem with raycasting to the mouse position for some reason

alright i’m going to try and use mouse.target instead, this will mean i have to change a lot of stuff, i’ll see if it works…

Well, you’re output says that your ray is actually hitting something. This means that your raycast works. There shouldn’t be any problems involving the mouse position. It’s hitting directly towards the dummy.

If you watch the video, with the raycast you can see I am clicking on the dummy, and I wasn’t able to show it but sometimes it would randomly hit other objects aswell.

Thankfully I have reworked a bit of stuff, and Mouse.Target is working much better! Now I just need to work on the bullet effect so it goes off everytime you fire, not just when you hit something.

The gun was not this smooth before -------^^^

:happy4:

1 Like