Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a mouse position on my gun? please help :P

Asked by 6 years ago
Edited 6 years ago

I have tried to get a mouse but it is not working here is my code:

01local tool = script.Parent
02 
03tool.Activated:Connect(function()
04    local playername = tool.Parent.Name
05    local player = game.Players:FindFirstChild(playername)
06    local Mouse = player:GetMouse()
07    local mouse = Mouse.Hit
08    local bullet = Instance.new("Part")
09    bullet.Anchored = bullet
10    bullet.CanCollide = false
11    bullet.BrickColor = BrickColor.new("Really red")
12    bullet.Material = ("Neon")
13    bullet.Parent = workspace
14    bullet.Position = tool.Handle.Position + Vector3.new(0, 0, 1)
15    bullet.CFrame = CFrame.new(bullet.CFrame.p,mouse.p)
16end)

the error I get is:

Workspace.Pistol.Script:7: attempt to index local 'Mouse' (a nill value)

0
Please format your code using the "lua" button (edit the question so it is clear to read), Also is this a local script or server script? SerpentineKing 3885 — 6y
0
This is a script inside of the tool aandmprogameing 52 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited by User#24403 6 years ago

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

01local player = game:GetService("Players").LocalPlayer
02local mouse = player:GetMouse()
03local tool = script.Parent
04 
05tool.Activated:Connect(function()
06    local bullet = Instance.new("Part")
07    bullet.Anchored = bullet
08    bullet.CanCollide = false
09    bullet.BrickColor = BrickColor.new("Really red")
10    bullet.Material = Enum.Material.Neon
11    bullet.Parent = workspace
12    bullet.Position = tool.Handle.Position + Vector3.new(0, 0, 1)
13    bullet.CFrame = CFrame.new(bullet.CFrame.p, mouse.Hit.p)
14end)
0
ok thank. you aandmprogameing 52 — 6y
Ad

Answer this question