My Raycast bullets goes though a wall when I point my mouse on the other side of the wall

. What do you want to achieve? Keep it simple and clear!’

  1. I want to make it where the position of my mouse on the other side of a wall the RayCast/Bullet will not go past it and stop where the wall is.

  1. My Bullets has a inf stud it only stops when there is a part that the mouse is hitting.

  1. When I shoot the sky the bullets don’t show.

  1. What is the issue? Include screenshots / videos if possible!

I tired looking on Youtube, Devforum and non of it worked. I’m still Kind of knew to Raycast. If there anything else i can learn please tell me. Ty have a good one

here my script

–Client Side

local testEven = game.ReplicatedStorage.GunEvents.TestGun:WaitForChild("TestEvent")
local ShootEvent = game.ReplicatedStorage.GunEvents.TestGun:WaitForChild("ShootEvent")
local UIS = game:GetService("UserInputService")
local RepStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() 
local Humanoid = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid")
local TweenService = game:GetService("TweenService")

local hold = Character:WaitForChild("Humanoid"):LoadAnimation(RepStorage.Animations.TestGun:WaitForChild("Zoom"))
local Shoot = Character:WaitForChild("Humanoid"):LoadAnimation(RepStorage.Animations.TestGun:WaitForChild("Shoot"))
local equiped = Character:WaitForChild("Humanoid"):LoadAnimation(RepStorage.Animations.TestGun:WaitForChild("Equipped"))

local Scope = script.Parent.Handle.Scope
local LaserPointer = script.Parent.Handle.LaserPoint.SpotLight
local GunSound = RepStorage.Sound.GunSund.TestGunSound.COM18_Suppressor
local beamSound = RepStorage.Sound.GunSund.TestGunSound.NightVision
local shoot = script.Parent.Handle.Shoot.Attachment

local cooldown = false
local Black = shoot.BlackSparks
local Circle = shoot.Circle
local Sparklies = shoot.Sparklies
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Enable = false
local on = false
local Cool = false
local debouce = false



script.Parent.Equipped:Connect(function()
	if Enable == false then
		Enable = true
		equiped:Play()
		Mouse.Icon = "rbxassetid://14344850185"
		print("Equipped")
	end
end)

script.Parent.Unequipped:Connect(function()
	Enable = false
	Mouse.Icon = "rbxasset://SystemCursors/Arrow"
	hold:Stop()
	print("Unequipped")
end)

UIS.InputBegan:Connect(function(input, p)
	if Enable == true and not p and input.UserInputType == Enum.UserInputType.MouseButton2 then
		if on == false then
			on = true
			
				Scope.Transparency = 0
				LaserPointer.Enabled = true
				Mouse.Icon = "rbxassetid://14344808579"
				hold:Play()
				print("Hold")
				testEven:FireServer("Pressed")
			
		end
	end
end)

UIS.InputEnded:Connect(function(input, G)
	if Enable == true and not G  and input.UserInputType == Enum.UserInputType.MouseButton2 then
		
		Scope.Transparency = 1
		LaserPointer.Enabled = false
		hold:Stop()
		print("Released")
		Mouse.Icon = "rbxassetid://14344850185"
		testEven:FireServer("presed1")
		on = false
	end
end)

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true
		Black:Emit(10)
		Circle :Emit(10)
		Sparklies:Emit(8)
		GunSound:Play()
		local Start = script.Parent.Handle.Shoot.Position
		local Distance = Mouse.hit.Position
		Shoot:Play()
		Character:FindFirstChild("Humanoid")
		Humanoid.WalkSpeed = 7
		wait(0.1)
		Humanoid.WalkSpeed = 16
		ShootEvent:FireServer(Start, Distance)	
		cooldown = false
	end
end)```

---Server Side
```wait()
local RepStorage = game:GetService("ReplicatedStorage")
local testEven = game.ReplicatedStorage.GunEvents.TestGun:WaitForChild("TestEvent")
local ShootEvent = game.ReplicatedStorage.GunEvents.TestGun:WaitForChild("ShootEvent")
local ServerStorage = game:GetService("ServerStorage")
local Ts = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Character = script.Parent:FindFirstAncestorOfClass("Character")
local ServerStorage = game:GetService("ServerStorage")


local shoot = script.Parent.Handle.Shoot.Attachment
local GunSound = RepStorage.Sound.GunSund.TestGunSound.COM18_Suppressor
local Scope = script.Parent.Handle.Scope
local LaserPointer = script.Parent.Handle.LaserPoint.SpotLight
local beamSound = RepStorage.Sound.GunSund.TestGunSound.NightVision


local Black = shoot.BlackSparks
local Circle = shoot.Circle
local Sparklies = shoot.Sparklies

local exclude = {}

testEven.OnServerEvent:Connect(function(player, Value1, Value2)
	if Value1 == "Pressed" then
		wait(0.2)
		beamSound:Play()
		print(Value1)
	end
end)


local function 	Bullet(Start, Distance)
	local center = Start + Distance/ 2
	local direaction = Distance.Magnitude
	
	local Bullet = Instance.new("Part")
	Bullet.Parent = workspace
	Bullet.Color = Color3.new(0, 0, 0)
	Bullet.Material = 'Neon'
	Bullet.Size = Vector3.new(0.1, 0.1, direaction)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.CFrame = CFrame.new(center, Start)
	
	Ts:Create(Bullet, TweenInfo.new(0.5), {Size = Vector3.new(0,0,direaction)}):Play()
		
	
	Debris:AddItem(Bullet, 0.5)
	return Bullet
end




ShootEvent.OnServerEvent:Connect(function(player, Start, End)

	local Character = player.Character
	local Distance = (End - Start)
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {player.Character}
	
	local ray = workspace:Raycast(Start, Distance.Unit * 300, params)

	if ray then
		print(ray)
		local hit = ray.Instance
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			if ray.Instance.Name == "Head" then
				Humanoid.Health -= 30
				print("Head")
			else
				Humanoid.Health -= 10
				print("Body")
			end
		end

	end	
	
	Bullet(Start, Distance)

end)```
1 Like

The issue is that “Distance” is mouse.hit.position. You should use a raycast towards the mouse hit position afterwards and check if it hits anything, in which if it does, the distance between the start of the raycast and the location it hits is the true distance you want

I kinda understand what you mean. But not fully because I don’t understand Raycast that much can you explain it more simpler if possible.

Step 1: Find mouse.hit.position
Step 2: Cast a raycast in that direction Raycasting | Documentation - Roblox Creator Hub (you need to find the rotation it needs to reach mouse.hit.position. You have 2 coordinates, you can use sin cos or tan to find the rotation
step 3: see if the raycast hits anything
step 4: if it does, make the bullet go to the hit instead

So the website you gave me i looked over it so many times. I Understand it but some of the stuff i just don’t understand. and there no way it need Sin and Cos. i hate alg is there another way? that can work?

A bare-bones gun you can make that doesn’t go thru walls is by just sending the direction vector from the gun to the mouse position into the server and the origin, so it can be verified by the server:

-- CLIENT --
local plr = game:GetService("Players").LocalPlayer
local remote = script.Parent.CheckHit
local tool = script.Parent
local BulletOrigin = tool.Handle.Attachment -- Attachment object

local Mouse
tool.Equipped:Connect(function(mouse)
   Mouse = mouse -- Player's mouse, equivalent of "Player:GetMouse()"
end
tool.Activated:Connect(function()
   local origin = BulletOrigin.WorldPosition
   local target = Mouse.Hit
   local direction = (target - origin) -- A vector determining the distance from origin to target.
   remote:FireServer("Fire",origin,direction)
end)
-- SERVER --
local P = game:GetService("Players")
local tool = script.Parent
local remote = tool.CheckHit
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude

local plr
tool.Equipped:Connect(function() -- Mouse doesn't work in server
   plr = P:GetPlayerFromCharacter(tool.Parent)
   if plr then
      params.FilterDescendantsInstances = {plr.Character} -- Avoid hitting the user itself.
   end
end)
tool.Unequipped:Connect(function() -- Mouse doesn't work in server
   plr = nil
end)

remote.OnServerEvent:Connect(function(caller,method,...)
   if caller ~= plr then return end
   -- Does nothing if the caller does not match with whoever is holding the tool.

   if method == "Fire" then
      local Origin,Direction = ...
      -- Verify if the player is *close* to it's reported origin
      local Pos = plr.Character.HumanoidRootPart.Position
      if (Origin - Pos).Magnitude > 2.5 then
         error("Origin is too far from the player's position!")
      end

      local checkDist = Direction.Unit * 5000 -- .Unit is the same Vector, but only preserves direction and length is now 1 unit.
      
      -- Proceed to perform the raycast once the checks has been done.
      local CastResult = workspace:Raycast(Origin,checkDist,params) -- Returns nil if hits nothing.
      if CastResult then
         locar hitPlr = P:GetPlayerFromCharacter(CastResult.Instance.Parent)
         if hitPlr then -- hitPlr is nil if it's parent isn't directly the character's model.
            -- You've hit someone, does something here.
         end
      end

   end
end)

Please note that this is just an example on how you would use the Raycast method in it’s basic form.