I'm trying to send a players user from a local script in StartGui to a server script in workspace using a RemoteFunction but I get 2 error messages, either 'attempt to index nil with InvokeServer' or 'attempt to index nil with OnServerInvoke'. I've used the devs hub reference to make this but I just can't figure out why it's not working.
local script:
local Player = game:GetService("Players").LocalPlayer.Name local Playeruse = game.Workspace:WaitForChild(Player) if Playeruse:FindFirstChild(Player) == true then print(Playeruse) end local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteEvent = ReplicatedStorage:WaitForChild("Remote") local mouse = game.Players.LocalPlayer:GetMouse() mouse.KeyDown:connect(function(key) key = key:lower() if key == "f" then print("Keypressed") local Playersname = Remote:InvokeServer(Playeruse) print("ServerFired") print(Playeruse) end end)
server script:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteFunction = ReplicatedStorage:WaitForChild("Remote") local function Playersname(Playeruse) print(player) local player = game.Workspace:FindFirstChild(Playeruse) local Headlamp = player:FindFirstChild("Headlamp") local LightEmitter = Headlamp:FindFirstChild("LightEmitter") local Spotlight = LightEmitter:FindFirstChild("SpotLight") if Spotlight.Enabled == false then Spotlight.Enabled = true else if Spotlight.Enabled == true then Spotlight.Enabled = false end end end Remote.OnServerInvoke = Playersname(Playeruse)
It does succeed in getting the players user, and registering the f key press, the only problem with both scripts is the RemoteFunction stuff, I would really appreciate it if someone could point out what I'm doing wrong. Thanks!
I believe it is because the player is already a passed argument in the InvokeServer() function, so you don't need to add playerUse to it.
I just should've used remote event instead, I can get the name now