Assign model to player

I’m making a house / plot system and I need to assign the house to the player so that when they leave the house will delete aswell. I’ve heard of collection service but so far I’m not to sure.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local House = ReplicatedStorage.House
local houseNum
local HousePlots = workspace.HousePlots.FreePlots:GetChildren()
local houses = {
	ReplicatedStorage.House1,
	ReplicatedStorage.House2
}

game.Players.PlayerAdded:Connect(function(player)
	
	wait(2)

	local random = math.random(1,#HousePlots)
	local data = player.house.HouseNum.Value
	
	local HouseClone = House:Clone()
	HouseClone.Parent = workspace
	HouseClone.LetterBox.Wedge.BillboardGui.TextLabel.Text = player.Name.. "'s House"
	
	local HousePlot = HousePlots[random]
	
	HouseClone.Main.CFrame = HousePlot.CFrame + Vector3.new(0,6.551,0)
	
	HousePlot.Parent = workspace.HousePlots.UsedPlots
	
	CollectionService:AddTag(HousePlot,player)
end)

i mean, is there any reason you can’t use a intvalue with a userID?

Tag the house so the game knows it’s for the player:
CollectionService:AddTag(HousePlot,player.Name)

Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(CollectionService:GetTagged(player.Name) -- find stuff with player tag
		v:Destroy()
	end
end)

or a table


--Table solution
local houses = {}
Players.PlayerAdded:Connect(function(player)
     houses[player] = houseModel
end)
Players.PlayerRemoved:Connect(function(player)
     houses[player]:Destroy()
end)

-- Object solution
Players.PlayerAdded:Connect(function(player)
     houseModel.Owner.Value = player -- where the Onwer is a object value
end)
Players.PlayerRemoved:Connect(function(player)
     for i,house in pairs(houses) do
            if house.Owner.Value == player then
                 house:Destroy()
            end
    end
end)
CollectionService:AddTag(House,player.Name)

Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(CollectionService:GetTagged(player.Name)) do
		v:Destroy()
	end
end)

Unless I did it wrong it didn't work

Did you just paste it in like that? That was supposed to be just a sample, try here:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local House = ReplicatedStorage.House
local houseNum
local HousePlots = workspace.HousePlots.FreePlots:GetChildren()
local houses = {
	ReplicatedStorage.House1,
	ReplicatedStorage.House2
}

game.Players.PlayerAdded:Connect(function(player)

	wait(2)

	local random = math.random(1,#HousePlots)
	local data = player.house.HouseNum.Value

	local HouseClone = House:Clone()
	HouseClone.Parent = workspace
	HouseClone.LetterBox.Wedge.BillboardGui.TextLabel.Text = player.Name.. "'s House"

	local HousePlot = HousePlots[random]

	HouseClone.Main.CFrame = HousePlot.CFrame + Vector3.new(0,6.551,0)

	HousePlot.Parent = workspace.HousePlots.UsedPlots

	CollectionService:AddTag(House,player.Name)
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(CollectionService:GetTagged(player.Name)) do
		v:Destroy()
	end
end)

I’m so sorry I can be a bit of an idiot sometimes

1 Like

No problem, happens, haha good day :slightly_smiling_face:

1 Like