Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

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?

0

This is a script inside of the tool

1 answer

✓ Accepted answer

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)

1 comment

0

ok thank. you

This discussion is preserved as it was. New answers, comments, and votes are closed.