Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
rabbi99 714 Moderation Voter
4 years 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?

4 answers

Log in to vote
0
Answered by
sydre 229 Moderation Voter
4 years 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 714 — 4y
0
Well, what is the reason taht you need to use the mouse in a server script? sydre 229 — 4y
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 714 — 4y
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 229 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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 714 — 4y
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago

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

Answer this question