Value is nil when player leaves for some reason

i want it that if the player leaves it grabs the value

For example, if the player leaves, it should automatically eliminate them but I always get a nil value because the player left is there’s any way to fix it

Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left")
	local roomName = player.RoomName.Value
	print(roomName)
	ReplicatedStorage.MultiplayerStats.PlayersDied.Value = ReplicatedStorage.MultiplayerStats.PlayersDied.Value + 1
	Dormitory.Room[roomName].Owner.Value = ""
	player.Eliminated.Value = true
end)

Is it a local or server script? Do both roomName and player.Name … " left" print() in the output?

You haven’t provided any context into which value is nil here. Can you shed some light?

the script is from the serverscript in the serverscriptservice

Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left")
	local roomName = player.RoomName.Value
	print(roomName)
	ReplicatedStorage.MultiplayerStats.PlayersDied.Value = ReplicatedStorage.MultiplayerStats.PlayersDied.Value + 1
	Dormitory.Room[roomName].Owner.Value = ""
	player.Eliminated.Value = true
end)

updated script.

What does the output say, when you hit play?

I got “[playername] left”

espically for my username, “Otiaks2356 left”

Set roomName to nil on top of: local roomName = player.RoomName.Value:

local roomName = nil
local roomName = player.RoomName.Value
 Otiaks2356 left  -  Server - GameHandler:82
  08:47:28.451    -  Server - GameHandler:85
  08:47:28.451  Room is not a valid member of Folder "Workspace.Dormitory"

Please clarify your original error

1 Like

Edit: What happens now?

-- print everything
Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left")
    print(player.Eliminated.Value)
    local roomName = nil
    print(roomName)
	local roomName = player.RoomName.Value
	print(roomName)
	ReplicatedStorage.MultiplayerStats.PlayersDied.Value = ReplicatedStorage.MultiplayerStats.PlayersDied.Value + 1
    print(ReplicatedStorage.MultiplayerStats.PlayersDied.Value)
	Dormitory.Room[roomName].Owner.Value = ""
    print(Dormitory.Room[roomName].Owner.Value)
	player.Eliminated.Value = true
    print(player.Eliminated.Value)
end)

Let’s wait for OP to provide us with the original output. That will be enough to give us a solid start

1 Like

roomName will be nil after this point [ local roomName = nil ] in the script above.

Do that last.

1 Like
  08:56:51.185  Otiaks2356 left  -  Server - GameHandler:82
  08:56:51.185  false  -  Server - GameHandler:83
  08:56:51.185  nil  -  Server - GameHandler:85
  08:56:51.185    -  Server - GameHandler:87
  08:56:51.185  1  -  Server - GameHandler:89
  08:56:51.185  Room is not a valid member of Folder "Workspace.Dormitory"  -  Server - GameHandler:90
  08:56:51.185  Stack Begin  -  Studio
  08:56:51.185  Script 'ServerScriptService.GameHandler', Line 90  -  Studio - GameHandler:90
  08:56:51.185  Stack End  -  Studio

The error is the most important part:

Room is not a valid member of Folder “Workspace.Dormitory”

Focus on determining why that is. roomName being an empty string is also concerning

1 Like

I forgot to add the s at the “Room” so "Dormitory.Rooms[roomName].Owner.Value = “” like Room is changed to Rooms

It does not print anything or true for player.Eliminated.Value at the end.

I keep getting this
09:02:32.284 is not a valid member of Folder "Workspace.Dormitory.Rooms"

That is because an error was raised

What if I made a folder about players and then add roomnamevalues in?