What might be wrong with the codes below which make the tycoon bug, and reset over and over after a player leaves the game?
When game left:
model = script.Parent.Factory backup = model:clone() function Regen() model:remove() wait(1) model = backup:clone() model.Parent = script.Parent model:makeJoints() end while true do wait(1.5) if script.Parent:WaitForChild("Factory").OwnerName.Value ~= "" then if not game.Players:FindFirstChild(script.Parent.Factory.OwnerName.Value) then Regen() end end end
Let's take a step back and think here.
What could we do differently besides a loop, to make it more effective?
We need to create a new script in Workspace that will set the owner's name to nothing in the event he leaves. This is simple, so I won't go into to much detail other than the comments I leave.
function FactoryFind(place, name) for _,v in pairs(place:GetChildren()) do if v.Name == "Factory" then -- Finding the factory if v.Factory.OwnerName.Value == name then v.Factory.OwnerName.Value = "" -- Setting name to nothing end else FactoryFind(v) -- If we didn't find factory here, go deeper. end end end game.Players.PlayerLeaving:connect(function (oldPlayer) -- Don't have access to wiki to make sure this is spelled/used right. FactoryFind(Workspace, oldPlayer.Name) end)
All we need to do is edit your current script to used the changed event. Let's first create the event:
Note: We are going to keep your regen and variables for consistency.
model = script.Parent.Factory backup = model:clone() ownerName = script.Parent.Factory.OwnerName function Regen() model:remove() wait(1) model = backup:clone() model.Parent = script.Parent model:makeJoints() end ownerName.Changed:connect(function (onChange) if ownerName.Value == "" then Regen end end)