Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why wont a Players name be put into TextLabel?

Asked by 3 years ago

I'm currently working on a scrip that fires a client when a player dies and sends the name of the player who died and sends the player's killer but for some reason, I'm unable to put the player's name into a text label, and I know it's not an issue from the server I've tested the server 3 times with prints so I'm suspecting from the print testing and the error I get that it's from the client

Players.Player2.PlayerGui.MenuHandler.KillStats:60: invalid argument #3 (string expected, got nil)

1ReplicatedStorage:FindFirstChild("Events").KillStatsUI1.OnClientEvent:Connect(function(Player, Playername)
2    local KillStatsClone1 = KillPopUp:Clone() ---everything except the 5 line works.
3    KillStatsClone1.Parent = script.Parent.MainGui
4    KillStatsClone1.Total.Text = "50"
5    KillStatsClone1.PlayerName.Text = Playername
6end)
0
Are you firing the player or player.Name? ScottVulpes 105 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago

Problem

This is a misunderstanding of RemoteEvents with the :FireClient() and .OnClientEvent.

The first argument of :FireClient() is the player you want to fire to, and arguments after the first are other arguments you want to pass to the LocalScript. It looks like this.

Script

1RemoteEvent:FireClient(MarkedTomato, 2, 4)

LocalScript

1RemoteEvent.OnClientEvent:Connect(function(num1, num2)
2   print(num1) --prints 2
3   print(num2) --prints 4
4end)

If you still don't understand, you can look at the description of the parameters of :FireClient() here.

Your actual error means that it expected a string but got nil, nil meaning nothingness.

Solution

Make a variable for the LocalPlayer.

Fixed LocalScript

01local Players = game:GetService("Players")
02local player = Players.LocalPlayer
03 
04ReplicatedStorage:FindFirstChild("Events").KillStatsUI1.OnClientEvent:Connect(function()
05    local KillStatsClone1 = KillPopUp:Clone()
06 
07    KillStatsClone1.Parent = script.Parent.MainGui
08    KillStatsClone1.Total.Text = "50"
09    KillStatsClone1.PlayerName.Text = player.Name
10end)
Ad

Answer this question