Why is it displaying my name?

I am currently using RemoteEvents for a gun I am making and for some reason it keeps printing my username, instead of the gun name. Can someone help?

Fires event from client

local gunName = "Glock 17"
event:FireServer(gunName)

Prints gunName on server fire

event.OnServerEvent:Connect(function(gunName)

print(gunName)

end)
19:16:36.701  CloutBl0x  -  Server - Script:6

The First Argument inside a RemoteEvent is the Player, add another argument:

event.OnServerEvent:Connect(function(Player, gunName)

print(gunName)

end)

The reason the First Argument being the Player is that you are sending Data from the Client to the Server, and on the Server, you cant access the Player, This only Applies for OnServerEvent

For OnClientEvent, You are sending Data from the Server to the Client, when firing, you have to specify what player its running for, or use RemoteEvent:FireAllClients() to fire for all Players

This is from the First Paragraph of the Documentation:

A RemoteEvent is designed to provide a one-way message between the server and clients, allowing Scripts to call code in LocalScripts and vice-versa. This message can be directed from one client to the server, from the server to a particular client, or from the server to all clients.

RemoteEvent Documentation


8 Likes

I don’t what I did wrong but I made it-

	local gunName = "Glock 17"
	local player = game.Players.LocalPlayer
	event:FireServer(player, gunName)

-and-

event.OnServerEvent:Connect(function(player, gunName)
	
	print(gunName)
	
end)

-and its still printing my username

Remove this. you are assigning your player to gunName

1 Like

Replace the RemoteEvent with a RemoteFunction and place these scripts:

--The Local Script
local RemoteFunction = script.Parent.RemoteFunction --Path of the RemoteFunction
local gunName = "Glock 17"

RemoteFunction.OnClientInvoke = function()
	return gunName
end
RemoteFunction:InvokeServer()
--The Server Script
local RemoteFunction = script.Parent.RemoteFunction --Path of the RemoteFunction

RemoteFunction.OnServerInvoke = function(Player)
	local gunName = RemoteFunction:InvokeClient(Player)
	print(gunName, Player)
end

There’s no need for a RemoteFunction if the local script doesn’t need any information returned.

What @DasKairo is trying to say is that FireServer automatically passes the player in as the first argument to OnServerEvent, so you don’t need to do it manually when you call the function. The code should look like this:

Local script:

local gunName = "Glock 17"
event:FireServer(gunName)

Server script:

event.OnServerEvent:Connect(function(player, gunName)
print(gunName)
end)
1 Like

The reason why it is doing this is because you have Player in your :FireServer(). You do not need “Player” in your :FireServer() because it already passes it automatically. So just remove the "Player " in the parentheses of :FireServer() and it should work.

Basically, what is happening is because the :FireServer() passes the player automatically, when you have :FireServer(Player, Classes), and then you have (Player, Classes) in your OnServerEvent, Classes in the :FireServer() is actually being assigned to the Player in the OnServerEvent. That’s why it’s printing your name.

If you found my answer helpful, be sure to mark it as the solution! :white_check_mark:

1 Like
local gunName = "Glock 17"
event:FireServer(gunName)
event.OnServerEvent:Connect(function(player, gunName)
	
	print(gunName)
	
end)

Client:

local gunName = "Glock 17"
event:FireServer(gunName)

Server:

event.OnServerEvent:Connect(function(player, gunName)

print(gunName)

end)

I just realized how dumb I am, thank you.

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