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)
1 | ReplicatedStorage:FindFirstChild( "Events" ).KillStatsUI 1. OnClientEvent:Connect( function (Player, Playername) |
2 | local KillStatsClone 1 = KillPopUp:Clone() ---everything except the 5 line works. |
3 | KillStatsClone 1. Parent = script.Parent.MainGui |
4 | KillStatsClone 1. Total.Text = "50" |
5 | KillStatsClone 1. PlayerName.Text = Playername |
6 | end ) |
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
1 | RemoteEvent:FireClient(MarkedTomato, 2 , 4 ) |
LocalScript
1 | RemoteEvent.OnClientEvent:Connect( function (num 1 , num 2 ) |
2 | print (num 1 ) --prints 2 |
3 | print (num 2 ) --prints 4 |
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
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer |
03 |
04 | ReplicatedStorage:FindFirstChild( "Events" ).KillStatsUI 1. OnClientEvent:Connect( function () |
05 | local KillStatsClone 1 = KillPopUp:Clone() |
06 |
07 | KillStatsClone 1. Parent = script.Parent.MainGui |
08 | KillStatsClone 1. Total.Text = "50" |
09 | KillStatsClone 1. PlayerName.Text = player.Name |
10 | end ) |