Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get a model to Regenerate multiple times?

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

0

What do you mean?

0

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

0

I don't know what you mean by:"Or anyone for that matter?".

0

I meant 'for anyone for that matter' in-case someone else reading this knows how to fix it

0

Oh ok. Did it work?

0

It still only works for 1 regeneration. I'm really confused as to why this happens.

0

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.

0

Then you might have a better idea of what you need to fix

0

You should also set the Parent of backup last. Not first. It will make your script more efficient if you do so.

0

It appears that after the first regeneration, the second regeneration is not called

0

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.

1 answer

✓ Accepted answer

I once did something like this. The method I used was having all the basic stuff in a table(you could have it in a folder) and replacing the tycoon which had its owner leave with the tycoon at the index of the tycoon's name. That explanation is a bit unclear so here is the script:

-- when the game starts:

spawn(function()
	
	for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do
	
		if v:IsA("Model") then
	
			savedTycoonsTable[v.Name] = v:Clone()
			
			local runTycoonDoor = require(moduleScripts.RunTycoonDoor)
			
			runTycoonDoor(v)
	
		end
		
	end
	
end)

--when the player leaves:

if plrInfo.OwnsTycoon.Value then
	
	local tycoonName = plrInfo.OwnedTycoon.Value
	local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
		
	tycoon:Destroy()

	local tycoonCopy = savedTycoonsTable[tycoon.Name]:Clone()
	local runTycoonDoor = require(moduleScripts.RunTycoonDoor)
		
	runTycoonDoor(tycoonCopy)
	tycoonCopy.Parent = game.Workspace.Tycoons

end

I hope this helps and have a great day scripting!

4 comments

0

The module script I required had nothing to do with it. You can use your own method for the owner claiming the tycoon.

0

That's an interesting way of going it, I didn't think of that before.

0

With the placement of my tycoon's in ReplicatedStorage, do you know of any way I can replace my tycoons multiple times without failure?

0

Or anyone for that matter?

This discussion is preserved as it was. New answers, comments, and votes are closed.