Tycoon Rebirth Bug

Hello Developers!
It seems when I clone and delete a tycoon place it doesnt allow the part collector to work.
code for part of rebirth:


function removeTycoonAndAddOriginal(tycoon,playerName)
	local plrStats = game.ServerStorage.PlayerMoney:FindFirstChild(playerName)
	if plrStats ~= nil then
		plrStats.OwnsTycoon.Value = nil
	end
	if tycoon then

        --Holds the original tycoons
		local backup = AllTycoons[tycoon.Name]:Clone()
		tycoon:Destroy()
		wait()
		local currencyToCollect = backup:FindFirstChild("CurrencyToCollect")
		if currencyToCollect ~= nil then
			currencyToCollect.Value = 0
		else 
			print("currencyToCollect == nil ----------------------->")
		end
		backup.Parent=script.Parent.Tycoons
		
	end
end

code for part collector:


function NoDelaySound(part,id)
		local Sound = Instance.new('Sound',part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
		Sound:Play()

	end

game.Players.PlayerAdded:Connect(function(player)
	for i,v in pairs(script.Parent.Essentials:GetChildren()) do
		if v.Name == "PartCollector" then
			v.Touched:connect(function(Part)
				if Part:FindFirstChild('Cash') then
					Money.Value = Money.Value + Part.Cash.Value
				Debris:AddItem(Part,0.1)
				warn(player.Name)
					local thestats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
					if thestats ~= nil then 
						NoDelaySound(script.Parent.Essentials.PartCollector, Settings.Sounds.Collect)
						thestats.Value = thestats.Value + Money.Value
					Money.Value = 0
				end
			
				end
			end)
		end
	end
end)

(trying out zeds kit, had to recode a lot)

I’m going to assume this, but, is the PartCollector instance parented to the Tycoon?

If it is, then you need to reinstate the .Touched event for the new Tycoon, that’s all.
I would also like to note you shouldn’t just add a .Touched connection every time a player joins, it should be more systematic than that.

Also,

  • :connect is deprecated and there’s no sense using it over :Connect
  • thestats.Value = thestats.Value + Money.Value can be shorted to thestats.Value += Money.Value

image

Along with that, what do you mean by reinstate?

Basically, reconnect Touched. I’ll write a fix for it really quickly, I assume the “Owner” value is an ObjectValue that holds a Player, yes?

Just a value that contains the user’s username

I am still confused on how to reconnect touch…

Try this code for your PartCollector script first.

local function NoDelaySound(part,id) -- Define things as local if you do not need them to be used before they're defined. Or just move them up in the script if you do.
	local Sound = Instance.new('Sound', part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
	Sound:Play()
end

local Tycoon = script.Parent
local Owner = Tycoon.Owner
local StatStorage = game.ServerStorage.PlayerMoney

local Player = game:GetService("Players"):FindFirstChild(Owner.Value)

Owner.Changed:Connect(function()
	Player = game:GetService("Players"):FindFirstChild(Owner.Value)
end)

for _,Object in pairs(Tycoon.Essentials:GetChildren()) do
	if Object.Name == "PartCollector" then
		Object.Touched:Connect(function(Part)
			if Part:FindFirstChild('Cash') then
				if Player ~= nil then
					Money.Value += Part.Cash.Value
					Debris:AddItem(Part, 0.1)
					warn(Player.Name)
					local thestats = StatStorage:FindFirstChild(Player.Name)
					if thestats ~= nil then 
						NoDelaySound(Object, Settings.Sounds.Collect)
						thestats.Value += Money.Value
						Money.Value = 0
					end
				end
			end
		end)
	end
end

If it works, we can move on to fixing the issue after rebirth.
If it doesn’t, let me know of any errors that come up (if there are any)

No errors, it just doesn’t collect.

Fixed… but
image

Hm. That’s odd, can you try this real quick then?

local function NoDelaySound(part,id) -- Define things as local if you do not need them to be used before they're defined. Or just move them up in the script if you do.
	local Sound = Instance.new('Sound', part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
	Sound:Play()
end

local Tycoon = script.Parent
local Owner = Tycoon.Owner
local StatStorage = game.ServerStorage.PlayerMoney

local Player = game:GetService("Players"):FindFirstChild(Owner.Value)

if Owner:IsA("ObjectValue") then
	Player = Owner.Value
end

Owner.Changed:Connect(function()
	if Owner:IsA("ObjectValue") then
		Player = Owner.Value
		return
	end
	Player = game:GetService("Players"):FindFirstChild(Owner.Value)
end)

for _,Object in pairs(Tycoon.Essentials:GetChildren()) do
	if Object.Name == "PartCollector" then
		Object.Touched:Connect(function(Part)
			if Part:FindFirstChild('Cash') then
				if Player ~= nil then
					Money.Value += Part.Cash.Value
					Debris:AddItem(Part, 0.1)
					warn(Player.Name)
					local thestats = StatStorage:FindFirstChild(Player.Name)
					if thestats ~= nil then 
						NoDelaySound(Object, Settings.Sounds.Collect)
						thestats.Value += Money.Value
						Money.Value = 0
					end
				else
					warn("Player is nil, meaning the Owner value either isn't a string or the Owner isn't being updated properly.")
				end
			end
		end)
	end
end

Oh? I need to see the entire error.

image

That doesn’t line up for me with my code, can you show me the script you’ve got at the moment?

local function NoDelaySound(part,id) -- Define things as local if you do not need them to be used before they're defined. Or just move them up in the script if you do.
	local Sound = Instance.new('Sound', part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
	Sound:Play()
end
local Money = script.Parent.Parent.Parent.CurrencyToCollect
local Debris = game:GetService('Debris')
local Tycoon = script.Parent.Parent.Parent
local Owner = Tycoon.Owner
local StatStorage = game.ServerStorage.PlayerMoney

local Player = game:GetService("Players"):FindFirstChild(Owner.Value)

Owner.Changed:Connect(function()
	Player = game:GetService("Players"):FindFirstChild(Owner.Value)
end)

for _,Object in pairs(Tycoon.Essentials:GetChildren()) do
	if Object.Name == "PartCollector" then
		Object.Touched:Connect(function(Part)
			if Part:FindFirstChild('Cash') then
				if Player ~= nil then
					Money.Value += Part.Cash.Value
					Debris:AddItem(Part, 0.1)
					warn(Player.Name)
					local thestats = StatStorage:FindFirstChild(Player.Name)
					if thestats ~= nil then 
						NoDelaySound(Object, 3020841054)
						thestats.Value += Money.Value
						Money.Value = 0
					end
				end
			end
		end)
	end
end

Here you go, that was my bad.

local function NoDelaySound(part,id) -- Define things as local if you do not need them to be used before they're defined. Or just move them up in the script if you do.
	local Sound = Instance.new('Sound', part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
	Sound:Play()
end
local Money = script.Parent.Parent.Parent.CurrencyToCollect
local Debris = game:GetService('Debris')
local Tycoon = script.Parent.Parent.Parent
local Owner = Tycoon.Owner
local StatStorage = game.ServerStorage.PlayerMoney

local Player = game:GetService("Players"):FindFirstChild(if Owner.Value == "" or Owner.Value == nil then "Roblox" else Owner.Value) -- FindFirstChild() errors if passed an empty string, my bad.

Owner.Changed:Connect(function()
	Player = game:GetService("Players"):FindFirstChild(if Owner.Value == "" or Owner.Value == nil then "Roblox" else Owner.Value)
end)

for _,Object in pairs(Tycoon.Essentials:GetChildren()) do
	if Object.Name == "PartCollector" then
		Object.Touched:Connect(function(Part)
			if Part:FindFirstChild('Cash') then
				if Player ~= nil then
					Money.Value += Part.Cash.Value
					Debris:AddItem(Part, 0.1)
					warn(Player.Name)
					local thestats = StatStorage:FindFirstChild(Player.Name)
					if thestats ~= nil then 
						NoDelaySound(Object, 3020841054)
						thestats.Value += Money.Value
						Money.Value = 0
					end
				end
			end
		end)
	end
end

Its not collecting the part and providing money

Alas I have been a moron twice now.

Okay, so, your Owner value is actually an ObjectValue that holds the Player object itself, so let me fix the code.

local function NoDelaySound(part,id) -- Define things as local if you do not need them to be used before they're defined. Or just move them up in the script if you do.
	local Sound = Instance.new('Sound', part)
	Sound.SoundId = "rbxassetid://"..tostring(id)
	Sound.Volume = 0.2
	Sound.RollOffMaxDistance = 50
	Sound:Play()
end
local Money = script.Parent.Parent.Parent.CurrencyToCollect
local Debris = game:GetService('Debris')
local Tycoon = script.Parent.Parent.Parent
local Owner = Tycoon.Owner
local StatStorage = game.ServerStorage.PlayerMoney

local Player = Owner.Value

Owner.Changed:Connect(function()
	Player = Owner.Value
end)

for _,Object in pairs(Tycoon.Essentials:GetChildren()) do
	if Object.Name == "PartCollector" then
		Object.Touched:Connect(function(Part)
			if Part:FindFirstChild('Cash') then
				if Player ~= nil then
					Money.Value += Part.Cash.Value
					Debris:AddItem(Part, 0.1)
					warn(Player.Name)
					local thestats = StatStorage:FindFirstChild(Player.Name)
					if thestats ~= nil then 
						NoDelaySound(Object, 3020841054)
						thestats.Value += Money.Value
						Money.Value = 0
					end
				end
			end
		end)
	end
end

Edit: And if this doesn’t work, your problem lies with how you’re setting up the Tycoon.

Firstly, make a folder for the parts called “Drops” in workspace, and parent them to that instead.

After that,
Wherever the parts are made, add this code:

PartVariable:SetAttribute("Owner", ReferenceToTycoon.Owner.Value.Name)

Then add this to your rebirth code:

-- Put this variable somewhere at the top of your script.
local Drops = workspace:WaitForChild("Drops")
-- Next, add this into your removeTycoonAndAddOriginal function:
for _, Drop in pairs(Drops:GetChildren()) do
  if Drop:GetAttribute("Owner") == playerName then
    Drop:Destroy()
  end
end