You can write your topic however you want, but you need to answer these questions:
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
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
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)
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
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)
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.