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

Why does it keep saying my mouse is nil when I already defined it? (Filtering Enabled)

I enabled Filtering Enabled so I'm adding RemoteEvents and such.

I added a function in my local script so that when I click, it fires the server. I then added the mouse as an argument in the FireServer() function.

player = game.Players.LocalPlayer
mouse = player:GetMouse()

rep = game:GetService('ReplicatedStorage')
reme = rep:WaitForChild('RemoteEvent')

mouse.Button1Down:connect(function()
	reme:FireServer(mouse)
end)

I wanted it to print "hello" when it clicks on the baseplate so I wrote this in the server script (The server script's name is "firer")

rep = game:GetService('ReplicatedStorage')
reme = rep:WaitForChild('RemoteEvent')

reme.OnServerEvent:connect(function(player, mouse)
	if mouse.Target == workspace.Baseplate then
		print 'hello'
	end
end)

For whatever reason, it keeps on erroring and saying

15:11:09.355 - ServerScriptService.firer:5: attempt to index local 'mouse' (a nil value)

Sorry if my script doesn't make much sense and the mistake is obvious. This is one of my first times working with FE.

Edit since people say it works: It also works for me when I play, but when I use the client and server test (F7), it says mouse is nil

2 comments

0

hm. It seems like your code is right...

0

did you tried on mouse write Mouse = game.player:GetMouse()

2 answers

✓ Accepted answer

My script works as intended using your code! Everything should be right, seems it's just one of those things.

What I would recommend is revising your code to get rid of any unconventional or depreciated syntax. This includes capitalizing :Connect() events, using game.Workspace, and adding brackets to your print()'s.

Server Script:

rep = game:GetService('ReplicatedStorage')
reme = rep:WaitForChild('RemoteEvent')

reme.OnServerEvent:Connect(function(player, mouse)
    if mouse.Target == game.Workspace.Baseplate then
        print("hello")
    end
end)

Local Script:

player = game.Players.LocalPlayer
mouse = player:GetMouse()

rep = game:GetService('ReplicatedStorage')
reme = rep:WaitForChild('RemoteEvent')

print(player.Name, mouse)
mouse.Button1Down:Connect(function()
    reme:FireServer(mouse)
end)

Remember, sometimes scripts can be finnicky. If all else fails, either add wait() commands after defining variables, or copy the code and re-enter it into a new script.

Good luck! Hope this helps!

4 comments

0

Well in the first place it works too when I press play, but when I use the server and client test (F7), it says that mouse is nil. But thanks for the tips

0

ah, gotcha. I'll see if I can keep running tests and find the elusive problem with you.

1

Changing the argument of FireServer() from just "mouse" to "mouse.Target" seemed to work. Thanks for the help!

0

I'm so glad you seem to have got it working!

try this code on mouse local Mouse = Player:GetMouse()

1 comment

0

sorry it's still nil

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