I’m trying to make a remote function pass a string value from the client to server, then in the server script print that string (the string being PlayerName). However, when I run this script, it doesn’t fire. Any idea why? Here’s the script and localscript:
Script:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
RemoteFunction.OnServerInvoke = function(PlayerName)
print(PlayerName)
end
LocalScript:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local PlayerName = "Player1"
RemoteFunction:InvokeServer(PlayerName)
When a client invokes the server, the server picks up one extra variable (the player who sent the invoke) as the first variable. My best guess is that you are trying to use the player instead of the string with their name. The first variable that the server picks up is the player who sent it. You would need to have
--Server side script
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(Player)
print(Player.Name)
end)
Remote Function
--Server side script
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")
RemoteFunction.OnServerInvoke = function(Player)
print(Player.Name)
return Player.Name -- Good habit
end)
Thanks for the help everyone! I found out that the problem was that I put the localscript inside of Workspace, not StarterPlayerScripts. And also thanks for letting me know to put “Player” in the parameters - it likely would’ve taken me a while to find out.
So, the problem is that I’d like to have this localscript placed in a part in Workspace. Is there any way to get this to work?
LocalScripts cannot be executed in workspace.
However, you can achieve a similar result. Example of changing a part’s color in client side:
--Your original idea of putting a local script
--in workspace.
--In this case, I use this path:
--'game>Workspace>Part>LocalScript'
script.Parent.Color = Color3.new(1,1,1)
--It won't work due to the limitation of LocalScript
You can do it in another way.
--This is a LocalScript placed inside PlayerScripts
local Part = game.Workspace:WaitForChild("Part",10)
Part.Color = Color3.new(1,1,1)
Ohh I see. So the thing is, I’m trying to put a localscript inside of a driveseat to find the occupant’s name (the player’s name when they sit on it), then put that name in a table, then send that table to a server script through use of a RemoteFunction. I’m not sure how I’d do that in this case.