I have currently been learning how to make raycasting guns on Roblox but I am currently having trouble getting the players mouse. My gun uses a local script to trigger an event. A script get the event when fired and makes a ray although I do not know how I should make the ray in the script position to the player that fired the events mouse.
local tool = script.Parent
tool.Shoot.OnServerEvent:Connect(function(player)
local mouse = player:GetMouse()
if mouse then
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.FormFactor = "Custom"
beam.Transparency = 0
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(100)
end
end
end
end)

1 comment
Idk what happened here but I iI don't normally use this site so sorry.