I’m making a simple raycast gun, but sometimes I click very fast and the raycast doesnt fire at all? It just doesn’t feel right when im using it.
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local cosmeticBulletTemplate = ReplicatedStorage.Items.CosmeticBulletTemplate
local tool = script.Parent
local handle = tool.Handle
local firePoint = handle.FirePoint
local events = tool.Events
local Configuration = require(tool.Configuration)
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.CollisionGroup = "Bullet"
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local function fireBullet(player: Player, mousePosition: Vector3)
print("Received Event")
local origin = firePoint.WorldPosition
local vectorDirection = (mousePosition - origin).Unit * 300
local raycastResult = Workspace:Raycast(origin, vectorDirection, raycastParams)
local intersection = raycastResult and raycastResult.Position or origin + vectorDirection
local distance = (origin - intersection).Magnitude
local cosmeticBullet = cosmeticBulletTemplate:Clone()
cosmeticBullet.Size = Vector3.new(0.1, 0.1, distance)
cosmeticBullet.CFrame = CFrame.lookAt(origin, intersection) * CFrame.new(0, 0, -distance/2)
cosmeticBullet.Parent = Workspace
task.delay(.25, function()
cosmeticBullet:Destroy()
end)
end
events.Fire.OnServerEvent:Connect(fireBullet)