How do i get player name?

Hello, I was making a modcall system and got a problem like this:
image

I’ve been using the remote event to get player name

Error is at line 7

Server script:

event.OnServerEvent:Connect(function(plr, reason, sender)
	game:GetService("MessagingService"):PublishAsync("LiveModcall", reason, sender)
end)

game:GetService("MessagingService"):SubscribeAsync("LiveModcall", function(reason, sender)
    reason = reason["Data"]
	sender = sender["Data"]--here the error
	for _,v in pairs(game.Players:GetPlayers()) do
		if v.IsMod.Value == true then
			if v.PlayerGui:FindFirstChild("ModcallNotify") then
				local gui = v.PlayerGui:FindFirstChild("ModcallNotify")
				gui.Frame.Visible = true
				gui.Frame.Reason.Text = reason
				gui.Frame.Username.Text = sender
				gui.Frame.PlayerImage.Image = image_format..sender
				gui.ModTicket.PopOut:FireClient(v)
			end
		end
	end
end)

Local script:

local sender = script.Parent.Parent.Parent.Name
event_2:FireServer(panel.InputBox.Input.Text, sender)

If you know what’s wrong help me please.

3 Likes

You are indexing the value “sender” and “reason” , those variables aren’t of type dictonary and will thus error. You don’t need to index it, so simply removing line 6 and 7 should resolve the issue.


Same problem.

You’re pointlessly sending the LocalPlayer through a remote on the client-side. The first argument of any remote call is the client who made the request, so get rid of the sender - it’s redundant.

event_2:FireServer(panel.InputBox.Input.Text)

Next thing is that you’re passing too many arguments to PublishAsync. Sender gets dropped because there is no third parameter to PublishAsync. There is only a topic and a message. Send a dictionary with your data instead. Also, get rid of the redundant sender argument again.

local MessagingService = game:GetService("MessagingService")

event.OnServerEvent:Connect(function(plr, reason)
    local message = {
        ["Sender"] = plr.Name,
        ["Reason"] = reason
    }
 
   MessagingService:PublishAsync("LiveModcall", message)
end)

Once this is done, tackle your receiving server simply by accessing the message dictionary in SubscribeAsync.

local MessagingService = game:GetService("MessagingService")

MessagingService:SubscribeAsync("LiveModcall", function(message)
    local sentData = message["Data"]
    local sender = sentData["Sender"]
    local reason = sentData["Reason"]
    -- rest of your code here
end)

I would’ve told you to go read documentation since that could’ve helped you resolve your issue easily, but the Developer Hub still seems to be having an outage for various articles. At least on my end, that’s how it is.

3 Likes

Sorry about that, I’ve corrected my answer.

The post above answers the same question and provides more corrections and tips, I suggest you read it instead.


I got an error!
This is line 18:

gui.Frame.Reason.Text = reason

I did everything what you’ve said.

1 Like

There is the current code

event.OnServerEvent:Connect(function(plr, reason)
	game:GetService("MessagingService"):PublishAsync("LiveModcall", reason)
end)

game:GetService("MessagingService"):SubscribeAsync("LiveModcall", function(reason)
    local sentData = reason["Data"]
    local sender = sentData["Sender"]
    local reason = sentData["Reason"]
	for _,v in pairs(game.Players:GetPlayers()) do
		if v.IsMod.Value == true then
			if v.PlayerGui:FindFirstChild("ModcallNotify") then
				local gui = v.PlayerGui:FindFirstChild("ModcallNotify")
				gui.Frame.Visible = true
				gui.Frame.Reason.Text = reason
				gui.Frame.Username.Text = sender
				gui.Frame.PlayerImage.Image = image_format..sender
				gui.ModTicket.PopOut:FireClient(v)
			end
		end
	end
end)
1 Like

Oops, sorry i didnt pasted this

local MessagingService = game:GetService("MessagingService")

event.OnServerEvent:Connect(function(plr, reason)
    local message = {
        ["Sender"] = plr.Name,
        ["Reason"] = reason
    }
 
   MessagingService:PublishAsync("LiveModcall", message)
end)

Everything works!

1 Like

I’m glad your script is working! Consider marking his post as the answer using the check icon (:ballot_box_with_check:) right below it. This will mark this thread as resolved. Thanks!

Oh, thanks. Completely forgot :smile:

1 Like