0

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

rabbi99 632
1 year ago

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?

0
sydre 214
1 year ago

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

0
Do you perhaps know a different way? rabbi99 632 — 1y
0
Well, what is the reason taht you need to use the mouse in a server script? sydre 214 — 1y
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. rabbi99 632 — 1y
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 sydre 214 — 1y
0

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..

0

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!

0
Yes thanks! rabbi99 632 — 1y