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

Regeneration script breaks tycoon buttons, how can i fix it?

So this is my first time im trying to create any game and code it by myself. My regeneration script just break buttons everytime after first player and i dont know whats the reason and how it happens. Explain in idiot language, please. Thank you for help!

-- regeneration script

local Original = script.Parent:FindFirstChild("Tycoon")

function reset()
 if Original then
    local backup = Original:clone()
    Original:destroy()
    wait(1)
    backup.Parent = script.Parent
 end
end

local Players = game:GetService("Players")

Players.PlayerRemoving:connect(reset)

-- function for buttons, maybe this could give you a clue of whats wrong

for i,v in pairs(Buttons:GetChildren()) do
	local Object = Items:FindFirstChild(v.Object.Value)
	if Object then
		local TestLOL = Object:Clone()
		Object:Destroy()		
		if v:FindFirstChild("Dependency") then

			v.Button.CanTouch = false
			v.Button.Transparency = 1
			v.Button.CanCollide = false
            v.Button.SurfaceGui.Enabled = false
			coroutine.wrap(function()
			if Items:WaitForChild(v.Dependency.Value) then 
				v.Button.CanTouch = true
				v.Button.Transparency = 0
				v.Button.CanCollide = true  
				v.Button.SurfaceGui.Enabled = true
			end
			end)()
		end
		v.Button.Touched:Connect(debounce(function(touch)
			if Credits.Value >= v.Price.Value and CheckOwner(touch) then
				Credits.Value -= v.Price.Value
				TestLOL.Parent = Items
				v:Destroy()
			end
		end))
	end
end

2 answers

✓ Accepted answer

it's because the button code has already run

try this

-- regeneration script

local Original = script.Parent:FindFirstChild("Tycoon")

function reset()
    if Original then
        local backup = Original:clone()
        Original:destroy()
        wait(1)
        backup.Parent = script.Parent
        for i, v in pairs(backup:FindFirstChild("Buttons"):GetChildren()) do -- adding this script to make it run again on the cloned version
            local Object = Items:FindFirstChild(v.Object.Value)
            if Object then
                local TestLOL = Object:Clone()
                Object:Destroy()
                if v:FindFirstChild("Dependency") then
                    v.Button.CanTouch = false
                    v.Button.Transparency = 1
                    v.Button.CanCollide = false
                    v.Button.SurfaceGui.Enabled = false
                    coroutine.wrap(function()
                        if Items:WaitForChild(v.Dependency.Value) then
                          v.Button.CanTouch = true
                              v.Button.Transparency = 0
                              v.Button.CanCollide = true
                              v.Button.SurfaceGui.Enabled = true
                          end
                    end)()
                end
                v.Button.Touched:Connect(debounce(function(touch)
                  if Credits.Value >= v.Price.Value and CheckOwner(touch) then
                    Credits.Value -= v.Price.Value
                    TestLOL.Parent = Items
                    v:Destroy()
                  end
              end))
            end
        end
    end
end

local Players = game:GetService("Players")

Players.PlayerRemoving:connect(reset)

-- This won't run in the cloned version because it has already run in the non cloned version

for i,v in pairs(Buttons:GetChildren()) do
    local Object = Items:FindFirstChild(v.Object.Value)
    if Object then
        local TestLOL = Object:Clone()
        Object:Destroy()        
        if v:FindFirstChild("Dependency") then

            v.Button.CanTouch = false
            v.Button.Transparency = 1
            v.Button.CanCollide = false
            v.Button.SurfaceGui.Enabled = false
            coroutine.wrap(function()
            if Items:WaitForChild(v.Dependency.Value) then 
                v.Button.CanTouch = true
                v.Button.Transparency = 0
                v.Button.CanCollide = true  
                v.Button.SurfaceGui.Enabled = true
            end
            end)()
        end
        v.Button.Touched:Connect(debounce(function(touch)
            if Credits.Value >= v.Price.Value and CheckOwner(touch) then
                Credits.Value -= v.Price.Value
                TestLOL.Parent = Items
                v:Destroy()
            end
        end))
    end
end

just comment if it's not working

5 comments

0

Regen script is in one model with tycoon, which in folder and has main script inside, so shouldnt it run again? or maybe it has to run after regen script at the start of the game?

0

or what if i keep raw version of the tycoon in server storage with turned off main script, so regen loads it fully raw and launches main script after it fully loads?

0

yes you could to that

0

keep the raw in the server storage that should work

0

yeah, its working, thank you for the answer!

Here's my regeneration script's final form (Almost.. Im too lazy and tired right now to add checking if its owner or not, so it triggers everytime someone leave)

local Original = game.ServerStorage.StoredTycoons.A1 -- link to the object you want to clone
local Players = game:GetService("Players") 
local Owner = script.Parent.Tycoon.Values.OwnerVal -- value that stores owner's name
Copy = Original:Clone()

Players.PlayerRemoving:connect(function(Player)
    if Copy.Parent ~= workspace then
       Copy.Parent = workspace
       script.Parent:Destroy()
    end
end)

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