I have tried to get a mouse but it is not working here is my code:
local tool = script.Parent tool.Activated:Connect(function() local playername = tool.Parent.Name local player = game.Players:FindFirstChild(playername) local Mouse = player:GetMouse() local mouse = Mouse.Hit local bullet = Instance.new("Part") bullet.Anchored = bullet bullet.CanCollide = false bullet.BrickColor = BrickColor.new("Really red") bullet.Material = ("Neon") bullet.Parent = workspace bullet.Position = tool.Handle.Position + Vector3.new(0, 0, 1) bullet.CFrame = CFrame.new(bullet.CFrame.p,mouse.p) end)
the error I get is:
Workspace.Pistol.Script:7: attempt to index local 'Mouse' (a nill value)
GENERAL PRACTICE
"Material" is an Enum
, not a string, so while it may work some of the time, it is not guaranteed.
Using :FindFirstChild()
on the Players
Service can be problematic if an object was placed in the Service that had the same name as the player.
ISSUES
While you can get the player this way, the only way to retrieve the mouse is on the Client, so you will have to alter this to be a LocalScript
REVISED LOCAL SCRIPT under the Tool
local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() local tool = script.Parent tool.Activated:Connect(function() local bullet = Instance.new("Part") bullet.Anchored = bullet bullet.CanCollide = false bullet.BrickColor = BrickColor.new("Really red") bullet.Material = Enum.Material.Neon bullet.Parent = workspace bullet.Position = tool.Handle.Position + Vector3.new(0, 0, 1) bullet.CFrame = CFrame.new(bullet.CFrame.p, mouse.Hit.p) end)