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

How come the playermouse is nil in serverscript but not in localscript?

Down here is my localscript. If I were to run this script in local script, it would print out 'instance'

player = game.Players.LocalPlayer
character = player.Character
mouse = player:GetMouse()
team = player.Team
hit = mouse.Hit
print(mouse)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local hitre = ReplicatedStorage:WaitForChild("hitre")
 
hitre:FireServer(mouse.Hit, team, player:GetMouse())

Down here is the (compromised) serverscript. If I were to run this, it would print out 'nil'

ReplicatedStorage = game:GetService("ReplicatedStorage")
UserInputService = game:GetService("UserInputService")

hitre = Instance.new("RemoteEvent",game.ReplicatedStorage)
hitre.Name = "hitre"
hitre.OnServerEvent:connect(function(player, hit, team, mouse)
print(mouse)
end)

Why is that?

4 answers

✓ Accepted answer

The player:GetMouse() event only works in local scripts. Doesn't matter if you try to parse it to a server script through a remote event, it won't work

4 comments

0

Do you perhaps know a different way?

0

Well, what is the reason taht you need to use the mouse in a server script?

0

I need mouse.Hit's position to make an NPC move to a point where the player clicks on. But I can't request scripts so I'll figure out myself.

0

well you can do all of that in a local script. Then when you have the position you want the NPC to move to you send it to a server script

well the thing is, LocalScripts run on the player's device..A mouse is "local" to a player, therefore b/c localscripts run on the player's computer, they can access the mouse.. on the other hand, the server is what connects all the players.. it doesn't control any aspects relating to the player's device.. that's why you can only detect Key down/up events only in localscripts..

Actually, the thing about mouses is that their local sided. A mouse on the players side will be different for each player and a server script handles all players. Local scripts can only handle mouses, as its a local element. Hope that helps!

1 comment

0

Yes thanks!

That's because local scripts control things on clients for e.g. Mouse, GUI, and other things on the CLIENT

While Server scripts controls pretty much everything except not clients

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