Hello!
I have a gun that works perfectly fine; it shoots and it uses raycasting so that if the ray comes in contact with a player, it damages them.
The problem is that I’m very new to raycasting and I don’t fully understand how to use it/ how it works and I would like to make it so that a bullet is rapidly fired. Here is my script containing the raycasting:
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePosition)
local Damage = script.Parent.Parent.Values.Damage -- How much damage the weapon does
local FiredSound = script.Parent.Parent.Pistol.Pistol.Fired -- The sound that is played when fired
local Handle = script.Parent.Parent.Handle -- Handle of the weapon (Where the bullet should ideally start)
-- Play FireSound to all players in a 115 stud radius
for Number, Value in pairs(game.Players:GetChildren()) do
local magnitude = math.abs((Handle.Position - Value.Character.HumanoidRootPart.Position).Magnitude)
if magnitude <= 115 then
FiredSound:Play()
end
end
-- Raycasting stuff
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(Handle.Position, (mousePosition - Handle.Position)*100, raycastParams)
if raycastParams then
if raycastResult then
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid.Health -= Damage.Value
end
end
end
end
end)
Any help is appreciated, thank you in advance!!