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!
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…
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
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
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…
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)
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)
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
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)