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

Mouse is a nil value?

I'm trying to make a gun without a local script but it keeps saying mouse is a nil value.

Here is my script.

local tool = script.Parent.Parent
repeat wait() until tool.Parent.Name ~= "Backpack"
if tool.Parent.Name ~= "Backpack" then
print("Equipped")
local user = tool.Parent
print("Character")
local player = game.Players:GetPlayerFromCharacter(user)
print("Player")
local mouse = player:GetMouse()
print("Got mouse!")

3 comments

0

Since you're using a LocalScript, you can just get the player through the Players Service using "game.Players.LocalPlayer" instead of getting the player through the tool.

0

No im not using a local script

0

if you want to get the player's mouse you need to do that in a local script according to the roblox developer hub https://developer.roblox.com/en-us/api-reference/function/Player/GetMouse

1 answer

✓ Accepted answer
local Tool = script.Parent -- our tool.
local Players = game:GetService("Players") -- player service
local LocalPlayer = Players.LocalPlayer -- retrieving the client player
local Mouse = LocalPlayer:GetMouse() -- getting our mouse

Tool.Equipped:Connect(function() -- on tool equipped
       Tool.Activated:Connect(function() -- on click
	   print(Mouse.hit) -- prints mouse hit
       end)
end)

Getting the players mouse needs to be done on the client, if you're trying to pass it over the server you'll need to utilize remote events in order to achieve that goal.

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

If this helps make sure to tell me.

1 comment

0

Thanks, this is exactly what I needed to know!

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