Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I prevent "fake" bullets from passing through walls?

Hi, so I made a basic raycast gun that also has a fake bullet system that shows where the ray hit. However, when I shoot from behind a wall, the fake bullet shows the location where I clicked on, instead of just hitting the wall and disappearing. Keep in mind that it only shows the bullet, it won't do damage when shot from behind a wall.

ServerScript


-- Variables
local Damage = script.Damage.Value
local Range = script.Range.Value
local Cooldown = script.Cooldown.Value

-- Main

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, FromP, ToP) -- FromP is the origin of the ray, ToP is the mouse position
	
	local ray = Ray.new(FromP, (ToP-FromP).unit * 100) -- Create ray
	local  FindPart, Position = workspace:FindPartOnRay(ray , Player.Character, true, true) -- FindPart is the object that the ray hit, Position is the player's char
	
local Dist = (FromP-ToP).magnitude -- Distance between FromP and ToP

if not Dist == Range then Dist = Range end -- if the distance isnt the same as then Range value, then set it to it

local Laser = Instance.new("Part") -- Create the part
Laser.Parent = workspace 
Laser.Anchored = true
Laser.CanCollide = false
Laser.Size = Vector3.new(0.1,0.1,Dist) -- The bullet itself, it uses the Distance variable listed above to display a bullet starting from the gun to the mouse pos 
Laser.BrickColor = BrickColor.new(255, 255, 0)
Laser.CFrame = CFrame.new(FromP, Position) * CFrame.new(0,0, -Dist/2) -- Hacky solution that I found online
Laser.Material = Enum.Material.Neon


if FindPart and FindPart.Parent:FindFirstChild("Humanoid") then FindPart.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage) -- Damage part
 wait(0.15)

  Laser:Destroy()
	
else
	
wait(0.15)	
Laser:Destroy()		
		
end		
end)
	

LocalScript


-- Variables

local Cooldown = script.Parent.ServerSide.Cooldown.Value
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

-- Main

local Debounce = false

script.Parent.Activated:Connect(function()
	
	if not Debounce then
		Debounce = true
	
	
game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Barrel.Position, Mouse.Hit.Position)

 wait(Cooldown)

Debounce = false

end	
end)

Additional videos to explain my problem better

Normal firing

Firing behind a wall

I'm not sure what I can do at all, as this is my first time working with raycasts like this.

1 answer

Try adding a debounce once it finds a part that is not Humanoid's part. Like:

local disableDamage = false

if FindPart.Parent:FindFirstChild("Humanoid") and not disableDamage then 

	FindPart.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage) -- Damage part
	wait(0.15)	 
	Laser:Destroy()
 
elseif FindPart then

	disableDamage = true
	wait(0.15) 
	Laser:Destroy()
	disableDamage = false   
  
end    

This discussion is preserved as it was. New answers, comments, and votes are closed.