Cannot clear players plot when leave

Hello!
So im making a tycoon game. And everythings is done… Except the part wheres the tycoon will clear it self when the players left. I made a code something like this:
Script1: (checks the plots owner datas)

wait()
local message = Instance.new("Hint",game.Workspace)
message.Text = "Running..."
game.Players.PlayerAdded:Connect(function()
	for i,v in pairs(game.Workspace:WaitForChild("Plots"):GetChildren()) do
		local owner = v:FindFirstChild("Owner")
		if owner then
			local lastowner = owner.Value
			if lastowner then
				lastowner = lastowner.Name
			end
			owner:GetPropertyChangedSignal("Value"):Connect(function()
				print("Got a signal about the plots data: Player left?")
				if owner.Value == nil then
					spawn(function()
						if lastowner then
							message.Text = ""..lastowner.." has left!!"
						end
						wait(1)
						for i,allparts in pairs(v:GetChildren()) do
							if allparts:IsA("Model") then
								allparts:Destroy()
							end
						end
						message.Text = "All parts has been cleared!"
					end)
				end
			end)
		end
	end	
end)

Script2: (when a players left. It will make the plotowner value to nil)



game.Players.PlayerRemoving:Connect(function(player)
	if plotfounded == true then
		print("Told data that it was nil")
		print(daplots.Owner.Value)
		daplots.Owner.Value = nil
	end
end)
game:BindToClose(function()
	wait(10)
end)

Script3: (just made a while ago) check every second on the plot if the owner value exists or no
If its not there then delete the plot

> while wait() do
> 	if script.Parent.Owner.Value == nil then
> 		for i,v in pairs(script.Parent:GetChildren()) do
> 			if v:IsA("Model") then
> 				v:Destroy()
> 			end
> 		end
> 	end
> end

This is just starting to frustrating me because SOME of them worked (when player rejoins the game it will be deleted) but SOME of them DIDNT work. I tried looking for forums but theres nothing related to the help. Please help!

Isn’t it much better to delete the plot on the PlayerRemoving event?

Yes tried it but it doesnt seems to work.So im finding a way to fixes it. Seems like everything doesnt work

Can you tell me what doesn’t work?, was it like the destroying the parts?

I used my alt account on the phone.Play testing while my character is in the pc placing items and leaves. The alt account is in mobile and waits for the players to leave. When i left, i check the alt account.the part is still there…

If you’re checking if the value in the for loops is a model, have the models been destroyed?

Tried using the Hint. Some of it does said that the script has runned and already cleared.Some said the same but the parts still there

Can you use this destroying method instead:

local Plot = ... -- This is the plot of the player who just left.

for _, Object in pairs(Plot:GetDescendants()) do
    if Object:IsA("Instance") then
        -- You would have a check to not delete the parts where you build a tycoon
        Object:Destroy()
    end
end
1 Like

Alright, im on my phone right now. I’ll be adding it. Been fixing this code for a long time!

1 Like

image
Update tried 3 times
The first 2 time was a success. But the 3rd time it was like this:
Im not sure whats going on. But my phone battery just died :rofl:
All I know that when you set all the plots owner to nil (the player is still in the game)
It is garunteed that the plot will get removed.
This is the code:

while wait() do
	if script.Parent.Owner.Value == nil then
		for i,v in pairs(script.Parent:GetChildren()) do
			if v:IsA("Model") then
				v:Destroy()
			end
		end
	end
end

Since my battery just died. I think i might need to test again…

I don’t recommend doing that, also have you tried what I said?

1 Like

Yep, tried it… Still didnt work :frowning:

Why don’t you instead store the tycoon instances in a table and then get the plot to destroy it when the player leaves, here is what I am trying to say:

local Plots = {}

Players.PlayerAdded:Connect(function(Player)
    Plots[Player.Name] = Plot -- being your plot, should be activated when the player claims a tycoon
end)

Players.PlayerRemoving:Connect(function(Player)
     if Plots[Player.Name] ~= nil then
         -- Destroy the plot
     end
end)

That’s what I thought at least.

2 Likes

have you done any debugging on the ones that havent worked? if so please show that and if not, I would start putting some in

1 Like

wait()

local message = Instance.new("Hint",game.Workspace)
message.Text = "Running..."
game.Players.PlayerAdded:Connect(function()
	for i,v in pairs(game.Workspace:WaitForChild("Plots"):GetChildren()) do
		local owner = v:FindFirstChild("Owner")
		if owner then
			local lastowner = owner.Value
			if lastowner then
				lastowner = lastowner.Name
			end
			owner:GetPropertyChangedSignal("Value"):Connect(function()
				print("Got a signal about the plots data: Player left?")
				if owner.Value == nil then
					spawn(function()
						if lastowner then
							message.Text = ""..lastowner.." has left!!"
						end
						wait(1)
						for i,allparts in pairs(v:GetChildren()) do
							if allparts:IsA("Model") then
								allparts:Destroy()
							end
						end
						message.Text = "All parts has been cleared!"
					end)
				end
			end)
		end
	end	
end)

Putten some in the topics.

Oh yeah I clearly forgot. The last time all the system worked. But for some reason the plot isnt getting destroyed. I’ll run a debug for the owners value on every plot and send a result

I personally cant see any debugging done here but ill explain. The way that I would recommend doing this is using break points and using the “step-over” tool to see where it is stopping when it doesnt work

1 Like
local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(plr)
	-- this isnt written perfectly and using repeat until isnt an amazing idea, but you get the concept
	
	local owner = game.workspace.Plots:FindFirstChild("Owner")
	repeat	
		owner = game.workspace.Plots:FindFirstChild("Owner")
		task.wait()
	until owner.Value == plr.UserId
	
	-- destroy stuff here
end)
1 Like

I think its impossibke since you can’t yield a PlayerRemoving function

you cant yield the player leaving as far as im aware, but you can still destroy the tycoon after the player themselves leaves

1 Like