I am creating a system that allows the player to still interact with ClickDetectors while using a Tool. I need to access the local player and mouse in a SERVER-side script. I am trying to make it so that when the item is unequipped, an enemy will ignore you. That part worked, but the ClickDetectors don't.
I have tried using RemoteEvents to pass along the local player and mouse target, but it didn't seem to work. The three scripts below are what I used:
Server-Side Script
local plr
local mouseTarget
game.ReplicatedStorage.Tool.OnServerEvent:Connect(function(player, mouseTarget2)
plr = player
mouseTarget = mouseTarget2
end)
print(plr, mouseTarget)
script.Parent.Equipped:Connect(function()
game.ReplicatedStorage.LanternEvents.Equipped:FireClient(plr)
end)
script.Parent.Unequipped:Connect(function()
game.ReplicatedStorage.LanternEvents.UnEquipped:FireClient(plr)
end)
script.Parent.Activated:Connect(function()
if mouseTarget:FindFirstChildWhichIsA("ClickDetector") then
if mouseTarget.Name == "Paper" then
game.ReplicatedStorage.PaperEvents.PaperEvent1:FireClient(plr)
plr.CameraMode = Enum.CameraMode.Classic
elseif mouseTarget.Parent.Name == "Keypad" then
game.ReplicatedStorage.KeypadEvent:FireClient(plr)
elseif mouseTarget.Parent.Name == "Lighter" then
game.Workspace.Values.CollectedLighter.Value = true
mouseTarget:Destroy()
else
game.Workspace.Values.Collected.Value += 1
mouseTarget:Destroy()
end
end
end)
Local Script:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseTarget = mouse.Target
while wait(0.01) do
game.ReplicatedStorage.Tool:FireServer(mouseTarget)
end
The Output is: Nil Nil.
Thank you in advance, Programming_Good_Games.
There is no understating how much of a bad idea this is.
Firstly, and most importantly, you should never be sending constant requests to the server since this will cause poor performance. You should instead Listen to the RunService Heartbeat event on the client and check if the mouse is over one of the ClickDetectors, and only then Fire a RemoteEvent to the server, where the server will do a sanity check and act upon it. You should almost never use a while wait(a very small number) loop since this does not take into account the Client's frame rate, so there will be a very high chance of you checking the exact same frame multiple times.
To continue, it is impossible to directly access the mouse from the server since the mouse only exists on the client.