Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can't access character from player on PlayerRemoving event?

There seems to be an issue upon accessing character from the PlayerRemoving event. The weird thing is that I can get to character from a ClickDetector.MouseClick call. Any idea as to why this is acting like this?

Here's my code:

game.Players.PlayerRemoving:Connect(function(player)
	print(player.Backpack)
	for i,v in pairs(player.Character:GetChildren()) do
		if v.ClassName == "Tool" and v:FindFirstChild("RoomNumber") ~= nil then
			v.Parent = player.Backpack
		end
	end
	for i1,v1 in pairs(player.Backpack:GetChildren()) do
		if v1.ClassName == "Tool" and v1:FindFirstChild("RoomNumber") ~= nil then
			for i2,v2 in pairs(script.Parent.RoomScreen.Rooms:GetChildren()) do
				if v2.Name == v1.RoomNumber.Value then
					v2.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
					return
				end
			end
		end
	end
end)

As you can see, this is a hotel room script and I want this to work because when a player leaves without checking out, I want to make the room available again. Any ideas as to why accessing character doesn't work?

Thanks for your help!

7 comments

0

Welp, that should work, what's wrong? any errors or just nothing happening?

0

There is an error. I go into test mode, get myself a room (the rest of the code works) and it says the room is booked (color is red). When I leave, I made it print a message so I know it is working, but I get this error: Workspace.CheckInSystem.CheckIn.StaffCheckIn.CheckIn.Screen.MainScreen.Script:84: attempt to index field 'Character' (a nil value)

0

Have you referenced the character as "Player.Character or Play.CharacterAdded:Wait()" ?

0

Player.Character. Should I do CharacterRemoving too?

0

yah, maybe as Psudar said the character doesnt exist yet, but that wouldnt make sense actually since i assume the character must've loaded since you checked the room and started playing

0

try CharacterRemoving yeah

0

Throws no error and does nothing.

2 answers

This might work, try getting the character like this

workspace[Player.Name]

I'm just guessing

1 comment

0

Nope. Says its doesn't exist.

Solved it myself after messing with it for a while. What I did is change PlayerRemoving to PlayerAdded. That way, it always registers when the character either dies or disconnects. Works now!

Thanks for all the answers on this question as well.

This discussion is preserved as it was. New answers, comments, and votes are closed.