Can't pass variables through a remote event

I keep getting 2 errors when trying to call values from a remote event " Type Error: (5,29) Unknown global ‘plr’ " and "Type Error: (5,34) Unknown global ‘UserinfoEvent’ "

Heres my code, am I just missing something?

Localscript :

local plr = game.Players.LocalPlayer

script.Parent.Parent.User:GetPropertyChangedSignal("Text"):Connect(function()
	
	local UserinfoEvent = script.Parent.Parent.User.Text


	local DirectedPlayer = game.Players:FindFirstChild(UserinfoEvent)
	

	if game.Players[UserinfoEvent].Parent == game.Players then
		
		
	
		
			Event:FireServer(plr, UserinfoEvent)
	end
end)


ServerScript : 

local Event = game.ReplicatedStorage.Fails.Checker



Event.OnServerEvent:Connect(plr, UserinfoEvent)

(below this doesnt matter since it wont run since the variables are an "unknown global")

Thats because the Variable is not within the Scope of the Events.

You are supposed to connect a function in between the brackets, not the parameters of the function.

local function onServerEvent(player, userInfoEvent)
    - code goes here
end
Event.OnServerEvent:Connect(onServerEvent)
2 Likes

That is true, however the main issue is the way how the OP thinks of using event connections.

OnServerEvent isnt a function, its a property of events, im trying to pass variables, not functions

Meaning? Its all within the same “process” you could call it, I can’t see what I’m missing.

That’s not how :Connect() works, :Connect() requires functions to return a Signal, It doesn’t just take in Variables, it never worked like that.
Remind me whenever you read about the functions and what they do.
This is basically what OnServerEvent does:

:Connect(func(player: Player, ...any) -> ()): RBXScriptConnection

func(player: Player, ...any) -> () means function(), within the parenthesis is the Player Argument along with a Tuple (Ordered List)
: RBXScriptConnection means it returns a Signal (or Connection)

RemoteEvent.OnServerEvent:Connect(function(Player, Arg1, Arg2)

end)
-- Alternative Route:
function Event(Player, Arg1, Arg2)

end

RemoteEvent.OnServerEvent:Connect(Event)

So when firing:

RemoteEvent:FireServer(Arg1, Arg2)

No, The Variable is not within the Scope of the Event, MEANING you CANNOT use it, because its unknown to the Code, You would use a Global Variable (not _G), but that would cause issues.

1 Like

i think you can also get the player this way

Event.OnServerEvent:Connect(plr:Player, UserinfoEvent)

Nevermind! You were right, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.