Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can't access character from player on PlayerRemoving event?

Asked by 5 years ago

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!

0
Welp, that should work, what's wrong? any errors or just nothing happening? starmaq 1290 — 5y
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) UnitedGateCompany 18 — 5y
0
Have you referenced the character as "Player.Character or Play.CharacterAdded:Wait()" ? Psudar 882 — 5y
0
Player.Character. Should I do CharacterRemoving too? UnitedGateCompany 18 — 5y
View all comments (3 more)
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 starmaq 1290 — 5y
0
try CharacterRemoving yeah starmaq 1290 — 5y
0
Throws no error and does nothing. UnitedGateCompany 18 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

This might work, try getting the character like this

workspace[Player.Name]

I'm just guessing

0
Nope. Says its doesn't exist. UnitedGateCompany 18 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question