Player name is nil

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    I want to check which player left and then set a value to an int value depending which player left

  2. What is the issue? Include screenshots / videos if possible!

    I keep getting the error that plr1.Name is nil, this isn’t the player I got with PlayerRemoving but a player I got from a character

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

    I haven’t done anything worth mentioning, just not even sure why it would be nil

Oh also, _G.jmp1 is a global character variable that doesn’t change for the duration of this script. I will show the code how I declared it below the one with the problem

local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local plr1 = Players:GetPlayerFromCharacter(_G.jmp1)
local plr2 = Players:GetPlayerFromCharacter(_G.jmp2)
local wpl = rs.WhichPlayerLeft
wpl = 0
wait()
Players.PlayerRemoving:Connect(function(player)
	print(player.Name)	
	print(plr1.Name) -- Error is here
	if plr1.Name == player.Name then   --Also got error here obviously
		wpl.Value = 1
		print("Duelist 1 left")
	end
	if plr2.Name == player.Name then  --Might also get it here since I got it above
		wpl.Value = 2
		print("Duelist 2 left")
	end
end)

duel1 and duel2 are players

_G.jmp1 = duel1.Character
_G.jmp2 = duel2.Character

Where have you defined the ‘duel1’ and ‘duel2’ characters?

They have been defined in a separate script in which I declared the _G.jmp = duel1.Character
Edit: This is how the code looks for it, I won’t show the full thing but you get the gist of it

if Player8 ~= nil then
	plr8 = game.Players:GetPlayerFromCharacter(Player8.Parent)
end
local x = math.random(y, 8)   --Used to pick 2 random players
if(x == 8) then
	duelist1 = plr8.Name
	duel1 = plr8
	duelseat1 = seat8
end

If you also wonder how I got the players, this is how
image

The characters may have not loaded yet at this point, so you need to yield a bit.

_G.jmp1 = duel1.Character or duel1.CharacterAdded:Wait()
_G.jmp2 = duel2.Character or duel2.CharacterAdded:Wait()

Edit: That is assuming duel1 and duel2 are players.

No, its very far into the script. To get to that point it takes around 20-30 seconds

Ah yes, of course. That is why boys and girls you should not use _G and opt instead for module scripts.

Your script is reading _G.jmp1 and _G.jmp2, before they have been set.

The solution would wholly depend on your other script and your program logic flow. But you need to make sure those both variables are set, before you run these lines:

local plr1 = Players:GetPlayerFromCharacter(_G.jmp1)
local plr2 = Players:GetPlayerFromCharacter(_G.jmp2)

Ah, I see. I’ll use a remote event to trigger the script to read the values at an exact point in time. Will reply how it goes

Edit: Seems this won’t work since I’m working with server scripts

I do not want to assume too much, but if you are using Remote Event, this probably won’t work.

_G is not shared between server and client, only between multiple server scripts or multiple local scripts. But if you want to use normal Bindable Event between server scripts, this will be the way to go.

EDIT: Typed before you responded. Yes, use BindableEvent, not the RemoteEvent.

1 Like

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