How Do I Detect If the local player is removed?

So am trying to make gui button that gives a certain room to a player, so the button would give the player a certain leaderstats number and that would be his room, when the player takes that room, No other player can get the same room, So my problem here is that i want to make the bool value of the room equals to false when the player leaves/is removed from the game, But every time when i try to do that, the game chooses a random player to remove there room ability instead of the player being removed,

To be exact i need to know how to get the local player when he is being removed, Here is the code:
local plr = game:GetService(“Players”)

plr.PlayerRemoving:Connect(function(player)

if player.leaderstats.Room.Value == 0 then
	print("NoValue")

elseif player.leaderstats.Room.Value == 102 then
	game.ReplicatedStorage.Removing.Remove102:FireServer()

elseif player.leaderstats.Room.Value == 101 then
	game.ReplicatedStorage.Removing.Remove101:FireServer()
end

end)

You’re better off handling this on the server. You could have ObjectValues in each room, with the value being the player that’s occupying it. Have a PlayerRemoving event and iterate over all the rooms to see if the player stored in the ObjectValue is no longer in the game, example code:

for _, room in pairs(workspace.Rooms:GetChildren()) do
local owner = room.Owner.Value and not room.Owner.Value.Parent
if not owner then --//If the player isn't in the game anymore
room.Owner.Value = nil --//Set ObjectValue to nil, no player owns it anymore.
end
end

Do this on a client and the room will still remain owned by the player from others view. Like what C# said, it’s a better option to do this on a server.

Name changed owner … Is this what you’re looking for

I use this program

while true do
wait(1.5)
if script.Parent.OwnerNameValue.Value ~= “No current owner” then
if not game.Players:FindFirstChild(script.Parent.OwnerNameValue.Value) then

script.Parent.OwnerNameValue.Value = “No current owner”
script.Parent.Screen.SurfaceGui.TextLabel.Text = “No current owner”
script.Parent.Door.CanCollide = true
script.Parent.Door.Transparency = 0

end
end
end

which value did you insert, int value?

can a string value also work? cause i have problems with ObjectValue.

Don’t hesitate to ask your questions

So, i tried it and it worked but the problem is that when i press the gui button to change it’s value to make it full, the value of it doesn’t change idk why?

My question is, why do you not handle these things in the server?

  1. The server can see the leaderstats
  2. There is a PlayerRemoving event in the Players, which can be used in the server
  3. You will never need to use a remote event when doing 1 and 2.

On my perspective, you are relying a lot on the client. Not only is this not efficient, but also dangerous. An exploiter can just fire that remote event, which is why it is important to handle all necessary logics in the server.

You are using gui button instead of the door

Because the gui where you use to pick a room is in the starter gui.

You are overcomplicating how it will be removed. If I were you, I would add a PlayerRemoving in the server. If the event is fired, I get the player’s room number and make that room to false, meaning no one owns that room.

well yes here is the problem , on player removing i can’t get the player’s that is being removed number
every time i try to get the number it would pick another random player’s room number and make it open not full.

May I see how your code looks like if you use PlayerRemoving in the server?

when ever a player is leaving the game, it picks a random player’s room number and removes it.
local plr = game:GetService(“Players”)

plr.PlayerRemoving:Connect(function(player)

if player.leaderstats.Room.Value == 0 then
	print("NoValue")

elseif player.leaderstats.Room.Value == 102 then
	game.ReplicatedStorage.Removing.Remove102:FireServer()

elseif player.leaderstats.Room.Value == 101 then
	game.ReplicatedStorage.Removing.Remove101:FireServer()
end

end)

I apologize, I did not understand the question

StringValue

local Players = game:GetService(“Players”)

local StringValue = game.Workspace.StringValue

Players.PlayerAdded:Connect(function(player)

print(player.Name)

StringValue.Value = player.Name

end)

Players.PlayerRemoving:Connect(function(player)

print(player.Name)

StringValue.Value = “No current owner”

end)

I just used the code that you gave me above and used the player.PlayerAdded function that you said, and it worked , now the player gets a room no other can take it, and when he leaves the room opens, I don’t know how to thank you enough, You Got The Solution.

I hope I have provided the necessary assistance

1 Like