Ad
Log in to vote
0

Why wont a Players name be put into TextLabel?

Asked by 22 days 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)

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

1 answer

Log in to vote
2
Answered by 22 days 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

RemoteEvent:FireClient(MarkedTomato, 2, 4)

LocalScript

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

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

local Players = game:GetService("Players")
local player = Players.LocalPlayer

ReplicatedStorage:FindFirstChild("Events").KillStatsUI1.OnClientEvent:Connect(function()
    local KillStatsClone1 = KillPopUp:Clone()

    KillStatsClone1.Parent = script.Parent.MainGui
    KillStatsClone1.Total.Text = "50"
    KillStatsClone1.PlayerName.Text = player.Name
end)
Ad

Answer this question