Models not deleting upon leave for the first time

You can write your topic however you want, but you need to answer these questions:
I’m trying to make a script to add a room whenever a player joins and removes it once they leave but keep it all server-sided.
While the joining function works flawlessly, the leaving is the problem. The first time a user joins a game it makes their room as intended but when they leave their room stays. However if that user was to rejoin again a new room would be added on-top of that still with their first ever room and so now they have 2 rooms that are tagged under them. This time though when they leave it removes one room but still leaves the original first room of that user. I’m not particularly sure why this is happening and was wondering if its to do with client issues or the script not being coded correctly.

Server Script:

local Lig = game:GetService("Lighting")
local RoomToCopy = Lig.RoomTemplate.Room
local Wrksp = game:GetService("Workspace")
local ExistingRooms = Wrksp.Rooms
local function cloneroom(plr)
	local NewRoom = RoomToCopy:Clone()
	NewRoom.Parent = ExistingRooms
	NewRoom.RoomOwner.Value = plr
end
local function destroyroom(plr)
	for i, v in pairs(ExistingRooms:GetChildren()) do
		if v.RoomOwner.Value == plr then
			v:Destroy()
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	cloneroom(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	destroyroom()
end)

Replace destroyroom() with destroyroom(plr)

2 Likes

I think you forgot to add the “plr” in this function?

1 Like

Did you forget to add “plr” between the two parentheses in “destroyroom()?”

1 Like

Turns out I overlooked to much and was being dumb thanks for the help

1 Like

Just a thing to make this shorter, the problem is already fixed so it doesnt really matter.

game.Players.PlayerAdded:Connect(cloneroom)
game.Players.PlayerRemoving:Connect(destroyroom)