I want to raycast in the direction of a player's mouse, however, the raycast does not go in the direction of the player's mouse and redirects itself to the nearest object (any object that it deems fit) in workspace. Also, the raycasts sometimes just don't hit anything at all seemingly at random.
local lt = require(script.Parent.GameFunctions) function shot(plr,pos,team) local beam = script.Parent.otherstuff.effects.Beam:Clone() local particle = script.Parent.otherstuff.effects.ParticleEmitter:Clone() local barrel = game.Workspace:FindFirstChild(plr.Name)["Laser Gun"].Handle.Part local dir = pos - barrel.Position local rayParam = RaycastParams.new() local hb = script.Parent.otherstuff.hitboxes.networkcomp:Clone() hb.creator.Value = plr hb.active.Value = true hb.Parent = game.Workspace rayParam.FilterDescendantsInstances = {workspace.LazerTagAssets, barrel.Parent.Parent, plr.Character} rayParam.FilterType = Enum.RaycastFilterType.Blacklist rayParam.CollisionGroup = "Default" rayParam.IgnoreWater = false local ray = workspace:Raycast(barrel.Position, dir.Unit*1000, rayParam) print("remote") if ray then print("loltest") local hitins = ray.Instance local hitpos = ray.Position hb.Position = hitpos beam.Parent = barrel local hitatt = Instance.new("Attachment") hitatt.Parent = hitins hitatt.WorldPosition = hitpos beam.Attachment0 = barrel.Attachment beam.Attachment1 = hitatt particle.Parent = hitatt if team == "o" then beam.Color = ColorSequence.new(Color3.new(1, 0.545098, 0.145098)) particle.Color = ColorSequence.new(Color3.new(1, 0.545098, 0.145098)) hb.Color = Color3.new(1, 0.545098, 0.145098) elseif team == "g" then beam.Color = ColorSequence.new(Color3.new(0, 1, 0)) particle.Color = ColorSequence.new(Color3.new(0, 1, 0)) hb.Color = Color3.new(0, 1, 0) end beam.Enabled = true particle.Enabled = true print("shot : " .. hitins.Name) if hitins.Parent:FindFirstChild("Humanoid") then local hitplr = game.Players:FindFirstChild(hitins.Parent.Name) lt.respawn(hitplr) lt.addpoints(game.Workspace.LazerTagAssets.values.scores:FindFirstChild(plr.Name), 1) end wait(0.1) hitatt:Destroy() particle:Destroy() beam:Destroy() hb:Destroy() else hb:Destroy() end end game.ReplicatedStorage.FireLaser.OnServerEvent:Connect(shot)
Client
local tool = script.Parent local Players = game:GetService("Players") local player = Players.LocalPlayer local mouse = player:GetMouse() local RESET_SECONDS = 1 local isTouched = false local t = "o" function fire() if not isTouched and tool.Enabled == true then isTouched = true script.Fire:Play() local cf3d = mouse.Hit game.ReplicatedStorage.FireLaser:FireServer(cf3d.Position, t) script.Reload:Play() wait(RESET_SECONDS) isTouched = false end end tool.Activated:Connect(fire)