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 5 years ago
Edited 5 years ago

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)

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 — 5y
0
This is a script inside of the tool aandmprogameing 52 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited by User#24403 5 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

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)
0
ok thank. you aandmprogameing 52 — 5y
Ad

Answer this question