Here is what my gun is currently doing:
Everything works fine until i put the gun into the wall and it starts shooting through it
Here is my code:
local debris = game:GetService("Debris")
local tool = script.Parent
local gunRemote = tool.GunRemote
local shootSound = tool.GunMesh.Shot
local config = tool.Configuration
local configValues = {
MaxDamage = config.MaxDamage.Value;
MinDamage = config.MinDamage.Value;
Range = config.Range.Value ;
}
local random = Random.new(tick())
local gunMesh = tool.GunMesh
local att1 = gunMesh.Att1
local beam = gunMesh.Beam
local function createBullet(targetPosition)
local att1Clone = att1:Clone()
local beamClone = beam:Clone()
att1Clone.Parent = workspace.Terrain
beamClone.Parent = gunMesh
att1Clone.WorldPosition = targetPosition
beamClone.Attachment1 = att1Clone
debris:AddItem(beamClone,0.06)
debris:AddItem(att1Clone,0.06)
end
local function onShoot(plr, mousePosition)
local rayOrigin = tool.RayOrigin.Position
local direction = (mousePosition - rayOrigin).Unit * configValues["Range"]
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {tool,plr.Character}
local raycastResult = workspace:Raycast(rayOrigin, direction, raycastParams)
if raycastResult then
createBullet(raycastResult.Position)
shootSound:Play()
local char = raycastResult.Instance:FindFirstAncestorWhichIsA("Model")
if char then
local hum = char:FindFirstChildWhichIsA("Humanoid")
if hum then
local damage = random:NextInteger(configValues["MinDamage"],configValues["MaxDamage"])
hum:TakeDamage(damage)
end
end
end
end
gunRemote.OnServerEvent:Connect(onShoot)
I’m not sure if I just didn’t code it right, but I tried to make a short raycast from the ray origin to check if it’s raycasting through a part and it didn’t work, here was my attempt:
local obstructionCheck = workspace:Raycast(rayOrigin, direction * 0.1, raycastParams)
if obstructionCheck then
return
end
Is there any other way to fix this?