I'm trying to find a way of regenerating a tycoon once the previous owner has left the game. The current owner's name is stored inside a 'StringValue' (location shown below) and I'm using that to check against players who have left to see if the tycoon should regen or not. While testing I noticed that this regeneration only works once before it will no longer regenerate at all. How can I fix this?
wait(1)
local model = game.ReplicatedStorage.Tycoon1
local floor = script.Parent
local backup = model:Clone()
backup.Parent = game.Workspace
backup.PrimaryPart = backup.Baseplate
wait(0.1)
backup:SetPrimaryPartCFrame(CFrame.new(floor.Position))
local PlayerName = backup.Main.Owner -- Location of StringValue
local function Regen()
backup:Destroy()
wait(0.1)
backup = model:Clone()
backup.Parent = game.Workspace
backup.PrimaryPart = backup.Baseplate
wait(0.1)
backup:SetPrimaryPartCFrame(CFrame.new(floor.Position))
end
game.Players.PlayerRemoving:Connect(function(Player)
print(Player.Name)
if Player.Name == PlayerName.Value then
Regen()
end
end)

11 comments
What do you mean?
I think that the best way to do it from replicated storage is to do :FindFirstChild(tycoonName) then to clone that child and put it wherever you want
I don't know what you mean by:"Or anyone for that matter?".
I meant 'for anyone for that matter' in-case someone else reading this knows how to fix it
Oh ok. Did it work?
It still only works for 1 regeneration. I'm really confused as to why this happens.
I think it might have something to do with how you are setting the value of PlayerName. Use some prints to see when regen is getting called.
Then you might have a better idea of what you need to fix
You should also set the Parent of backup last. Not first. It will make your script more efficient if you do so.
It appears that after the first regeneration, the second regeneration is not called
Ok then I bet you are somehow forgetting to set the PlayerName's value. Try seeing if that is the problem. Because of the if statement.