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)
u cant actually obtain the mouse from a server script(meaning non - LocalScripts); u can only request for it via a client script(meaning a LocalScript) and then transfer its desired data(in this case the mouses location) to a server script via limited means(like RemoteEvents, etc(which was why they exist in the first place; so that they allow communication between client(meaning player) and server in a game. Why do they want to restrict the server from easily obtaining client data? in case if u didnt know, simple answer would be that those pesky exploiters would get to exploit more easily)). So what we need to do is a slight alteration to ur client script and server script such that the client would send the mouse coordinates and that the server would receive them. Unfortunately u did not send me ur client script so i am unable to give u edits on that, instead i will just give u an example of a basic ray casting script that u can alter to fit into ur tool:
place in a Script in StarterPack:
local re = Instance.new("RemoteEvent") re.Parent = script re.Name = "re" re.OnServerEvent:Connect(function(plr, mouseHit) local root = plr.Character:FindFirstChild("HumanoidRootPart") if root then local ray = Ray.new(root.Position, (mouseHit.p - root.Position).Unit * 300) local part, pos = workspace:FindPartOnRay(ray, plr.Character, true, false) local beam = Instance.new("Part") beam.Parent = workspace beam.Anchored = true beam.CanCollide = false local dist = (root.Position - pos).Magnitude beam.Size = Vector3.new(.3, .3, dist) beam.CFrame = CFrame.new(root.Position, pos) * CFrame.new(0, 0, -dist / 2) game:GetService("Debris"):AddItem(beam, .1) if part then local hum = part.Parent:FindFirstChild("Humanoid") if hum then hum:TakeDamage(100) end end end end)
place in LocalScript in said Script:
local re = script.Parent:WaitForChild("re") local mouse = game.Players.LocalPlayer:GetMouse() mouse.Button1Down:Connect(function() re:FireServer(mouse.Hit) end)
as u can see, this is basically just a copy of ur server script, apart from the fact that the this one has been made to receive the mouse position and that my local script has been made to transmit it. u will also realize i substituted the tools handle for the players root part, as this is a standalone system, but u can later swap that with the tools handle.