Help needed using "Remote Events" to send the players username from client to server

So for context I’m attempting to make a cashier system (similar to the one in Work at a Pizza Place) and I’m having some trouble sending the player’s username to the server whenever he/she clicks the correct button on the menu tablet. I’ve tried multiple different “solutions” and none have seemed to have worked for me, any help would be greatly appreciated.

Server script:

cashierEvent.OnServerEvent:Connect(function(player)
	print(player)
end)

Local script:

local tablet = game.Workspace.Cashier.Tablet
local orderValue = tablet.orderValue
local guiFrame = script.Parent.Main
local cashierEvent = game.ReplicatedStorage.Events.CashierCorrect
local player = game.Players.LocalPlayer

function clicked(whichButton)
	if whichButton.Name == orderValue.Value then
		cashierEvent:FireServer(player, player.Name)
	else
		print("INCORRECT!!!")
	end
end

for _, button in ipairs(guiFrame:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		clicked(button)
	end)
end

Hierarchy:

4 Likes

okay ive got three ways that might fix this:

-1

change player to player.Name

if that doesnt work then

  1. You dont need to fire player.Name as the player already has the name

  2. Get rid of player in the server script and player.Name as the player parameter is practically invisible in the serverscript and will always be there at least i think so

1 Like

im not sure these will work as i have had this issue before i just forgot how i fixed it

1 Like

You don’t need the first argument / parameter of “player” because that’s automatically provided on the server.

And you really also don’t need the “player.Name” parameter either because you can just do that on the server. Here’s your fixed code:

cashierEvent.OnServerEvent:Connect(function(player)
   print(player.Name)
end)

local tablet = game.Workspace.Cashier.Tablet
local orderValue = tablet.orderValue
local guiFrame = script.Parent.Main
local cashierEvent = game.ReplicatedStorage.Events.CashierCorrect
local player = game.Players.LocalPlayer

function clicked(whichButton)
	if whichButton.Name == orderValue.Value then
		cashierEvent:FireServer()
	else
		print("INCORRECT!!!")
	end
end

for _, button in ipairs(guiFrame:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		clicked(button)
	end)
end
1 Like

You don’t need to send the player when firing to the server. The server receives the player as its first argument automatically. Also, just access the name of the player on the server.

Try this:

Server script:

cashierEvent.OnServerEvent:Connect(function(player)
	print(player.Name)
end)

Local script:

local tablet = game.Workspace.Cashier.Tablet
local orderValue = tablet.orderValue
local guiFrame = script.Parent.Main
local cashierEvent = game.ReplicatedStorage.Events.CashierCorrect
local player = game.Players.LocalPlayer

function clicked(whichButton)
	if whichButton.Name == orderValue.Value then
		cashierEvent:FireServer() -- no need to send player object as it is done automatically
	else
		print("INCORRECT!!!")
	end
end

for _, button in ipairs(guiFrame:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		clicked(button)
	end)
end
1 Like

Hello, the server script should be like this

cashierEvent.OnServerEvent:Connect(function(player,PlayerName)
	print(player)
 print(PlayerName)
end)

The first thing that comes to the server is the player who sent this message. Also, there is an error in the client script. You shouldn’t specify who sends the event; instead, add variables directly

cashierEvent:FireServer(player.Name)

If we’re talking about the complete view, it should be like this

Server

cashierEvent.OnServerEvent:Connect(function(player,PlayerName)
	print(player)
print(PlayerName)
end)

Local

local tablet = game.Workspace.Cashier.Tablet
local orderValue = tablet.orderValue
local guiFrame = script.Parent.Main
local cashierEvent = game.ReplicatedStorage.Events.CashierCorrect
local player = game.Players.LocalPlayer

function clicked(whichButton)
	if whichButton.Name == orderValue.Value then
		cashierEvent:FireServer(player.Name)
	else
		print("INCORRECT!!!")
	end
end

for _, button in ipairs(guiFrame:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		clicked(button)
	end)
end
1 Like

Hey, thanks for trying to help. However still nothing is being printed. Any ideas on why?

1 Like

Thanks for also trying to help, however this hasn’t seemed to have worked. I could possibly make this a model so you can take a more in-depth look if you would like?

That’d be good. It’s hard to tell what may be wrong from the short snippet. For example, not sure what the value this equals:

1 Like

Made it uncopylocked, so that it’s easier to figure out my exact layout.

Link: Town thing - Roblox

Once again, thank you so much for trying to help me!

1 Like

Make a server script like this

After the loop, the script doesn’t continue its execution until the loop is finished

local chatService = game:GetService("Chat")

local customer = script.Parent.Customer
local orderValue = script.Parent.Tablet.orderValue
local cashierEvent = game.ReplicatedStorage.Events.CashierCorrect

local orderChoice

cashierEvent.OnServerEvent:Connect(function(Player)
	print("Evenmt")
end)

while task.wait(5) do
	orderChoice = math.random(1, 4)
	local function numberToString()
		if orderChoice == 1 then
			orderChoice = "Drink"
		elseif orderChoice == 2 then
			orderChoice = "Burger"
		elseif orderChoice == 3 then
			orderChoice = "Chips"
		elseif orderChoice == 4 then
			orderChoice = "Pizza"
		end
		orderValue.Value = orderChoice
	end
	numberToString()
	chatService:Chat(customer.Head, orderChoice)
end

Inside ServerScriptService create a new script add this inside the script:"

-- Define the event
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local eventFolder = ReplicatedStorage:WaitForChild("Events") -- Replace "FolderName" with the actual name of the folder
local cashierEvent = eventFolder:WaitForChild("CashierCorrect")

-- Function to handle the event
local function handleCashierEvent(player)
	print("Cashier event fired for player: " .. player.Name)
end

-- Connect the event to the handling function
cashierEvent.OnServerEvent:Connect(handleCashierEvent)

Will work as intended

1 Like

Thank you soo much! Any idea why none of the other solutions worked? From my point of view this seems like a very random fix and doesn’t really explain why all the other suggestions didn’t work, although I guess Roblox is random like that sometimes. Anyway thank you very much for fixing this issue!

Is not a random fix. You need a server script to handle a remoteEvent. You can’t just create a remote event. Every remote event must be handled by a server script. You did not have a server script in your game, nothing would work until you created the server script and wrote the code to handle the remote event.

About the other solutions they did not reference the RemoteEvent folder correctly. They did not know you used a folder for your remote event.

I was using a server script to originally handle the remote event (the one in Workspace) and all the other responses did correctly recognize the RemoteEvent was in a folder and correctly referenced it.

No fried, nobody said that first you must load the folder:

local eventFolder = ReplicatedStorage:WaitForChild("Events")

Then use the folder to get the Event

local cashierEvent = eventFolder:WaitForChild("CashierCorrect")

And was this folder the reason code was not working.

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