Tycoon script issue

i am using a packaged script for many tycoons. it is supposed to act independently for each tycoon. i have 4 different kinds of tycoons, their main difference is that they are referencing different structures. earth, venus, mars, ceres. they have different buttons and objects.

so the issue is, when i spawn in, only ONE of them activates, and this only working tycoon is random. that means when player chooses a tycoon to put player in, some random tycoon nearby gets activated instead. assistant says it maybe happening in case many scripts reference one instance, but HOW can this be a problem???

script:

local tycoon = script.Parent
local ownerValue = tycoon.owner
local tycoonButtonsContainer = tycoon.buttonsContainer
local tycoonObjectsContainer = tycoon.objectsContainer
local buildSound = script.buildsound
local serverStorage = game:GetService('ServerStorage')
local tycoonSaveName: 'с Π·Π°Π³Π». Π±ΡƒΠΊΠ²Ρ‹' = tycoon.planet.Value --ΠΏΠΎ этому названию Π±ΡƒΠ΄Π΅Ρ‚ Π½Π°Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ ΠΏΠ°ΠΏΠΊΠ° для сохранСний
local plot = tycoon.plot
local referenceTycoonModel = tycoon.referenceTycoonModel.Value --ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ модСль-рСфСрСнс Ρ‚Π°ΠΉΠΊΡƒΠ½Π° Π² сСрвСрном Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅
local claimSpotButton = referenceTycoonModel['CLAIM SPOT']:Clone()
claimSpotButton.CFrame = plot.CFrame:ToWorldSpace(referenceTycoonModel.plot.CFrame:ToObjectSpace(claimSpotButton.CFrame))
claimSpotButton.Parent = tycoon
local buttonOriginTransparency = claimSpotButton.Transparency

if not referenceTycoonModel then warn('TYCOON REFERENCE NOT ASSIGNED') return end

local afkIncomequantities, afkIncomeCfg = tycoon.incomeStats, tycoon.incomeParams
local serverBuild = tycoon.serverBuild
task.wait(.5)

function clearObjects(): "ΠΈΠ·Π±Π°Π²ΠΈΡ‚ΡŒΡΡ ΠΎΡ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ²"
	for index, object in pairs(tycoonObjectsContainer:GetChildren()) do
		object:Destroy()
	end
end
function checkUp(object, taebl): "ΠΏΠΎΠ΄Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊ Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ"
	for i, part in pairs(object:GetChildren()) do
		if part.ClassName == 'Part' or part.ClassName == 'UnionOperation' or part.ClassName == 'MeshPart' then
			table.insert(taebl, part)	
		elseif part.ClassName == 'Model' then
			checkUp(part, taebl)
		end
	end
end
function addIncome(button): 'Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ 1 ΠΊ количСству Π°Ρ„ΠΊ Ρ„Π°Ρ€ΠΌΠΈΠ»ΠΎΠΊ, Ссли такая Π±Ρ‹Π»Π° Ρ€Π°Π·ΠΌΠ΅Ρ‰Π΅Π½Π°'

	local object = tycoonObjectsContainer:FindFirstChild(button.Name)
	--iterate through all build classes in afk income folder
	for index, objectClass in pairs(afkIncomequantities) do
		if object:HasTag(objectClass.Name) then
			objectClass.Value += 1
		end
	end
end
function animation(object, button): "анимация ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π΄Π΅Ρ‚Π°Π»ΡŒΠΊΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°"
	local soundClone = script.animSound:Clone()
	soundClone.Parent = button
	soundClone:Play()

	local parts = {}
	checkUp(object, parts)
	local transparencyTable = {}
	local collisionTable = {}
	for t,part in pairs(parts) do
		table.insert(transparencyTable, part.Transparency)
		table.insert(collisionTable, part.CanCollide)
	end
	for i,part in pairs(parts) do

		part.Transparency = 1
		part.CanCollide = false
	end
	for i,part in pairs(parts) do

		local relativeCFrame = referenceTycoonModel.plot.CFrame:ToObjectSpace(part.CFrame)
		local targetCFrame = plot.CFrame:ToWorldSpace(relativeCFrame)

		local duration = 3 / #parts
		local tween = game.TweenService:Create(part, TweenInfo.new(duration, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Transparency = transparencyTable[i], CFrame = targetCFrame})
		part.CFrame = ownerValue.Value.Character.Head.CFrame
		part.CFrame *= CFrame.Angles(math.random(-180,180), math.random(-180,180), math.random(-180,180))
		tween:Play()
		tween.Completed:Wait()
		part.CanCollide = collisionTable[i]
		task.wait(duration * 0.5)
	end
	soundClone:Destroy()
end
function switchButton(button, enabled): "ΠΎΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚ΡŒ\ΡΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΊΠ½ΠΎΠΏΠΊΡƒ"
	if enabled == true then
		button.Transparency = buttonOriginTransparency
	else
		button.Transparency = 1
	end
	button.CanTouch = enabled
	button.ButtonBillboard.Enabled = enabled
	button.CanCollide = enabled
end
function updateButtons()
	--ΠΏΠ΅Ρ€Π΅Π±Ρ€Π°Ρ‚ΡŒ ΠΊΠ½ΠΎΠΏΠΊΠΈ
	for i, button in pairs(tycoonButtonsContainer:GetChildren()) do

		local associatedObject = tycoonObjectsContainer:FindFirstChild(button.Name)
		local previousObject
		local parentValue = button.par.Value
		if parentValue then
			previousObject = tycoonObjectsContainer:FindFirstChild(parentValue.Name)
		end

		if associatedObject then --if this button was bought before the check
			switchButton(button, false)
		else --if this button was not bought before the check
			if previousObject or button.par.Value == nil then --if previous object bought or there is not supposed to be one
				switchButton(button, true)
			else
				switchButton(button, false)
			end
		end
	end
end
--ΠΏΠΎΠ΄Π³ΠΎΡ‚ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠ½ΠΎΠΏΠΊΠΈ ΠΊ Ρ€Π°Π±ΠΎΡ‚Π΅ послС запуска сСрвСра
for i, button in pairs(referenceTycoonModel.buttonsContainer:GetChildren()) do

	local relativeCFrame = referenceTycoonModel.plot.CFrame:ToObjectSpace(button.CFrame)
	button.CFrame = plot.CFrame:ToWorldSpace(relativeCFrame)
	print(plot.Parent.Name, plot.CFrame)
	local buttonType = 'money'
	local currencyIcon = 'πŸ’΅'
	if button:FindFirstChild('buttonGemModificator') then currencyIcon = 'πŸ’Ž' buttonType = 'gems' end
	if button:FindFirstChild('buttonMatterModificator') then currencyIcon = '🌌' buttonType = 'matter' end
	button.ButtonBillboard.TextLabel.Text = button.Name..' for '..button.cost.Value..currencyIcon
	button.ButtonBillboard.Enabled = true
	button.Parent = tycoonButtonsContainer
	button.Touched:Connect(function(touch)
		local thisButton = button
		local player = game.Players:GetPlayerFromCharacter(touch.Parent)

		if ownerValue.Value ~= nil or serverBuild.Value == true then
			if player.Name == ownerValue.Value.Name or serverBuild == true then
				local leaderstats = player:WaitForChild('leaderstats')
				local playerMoney = leaderstats:WaitForChild('money')
				local playerGems = leaderstats:WaitForChild('gems')
				local playerDarkMatter = leaderstats:WaitForChild('matter')

				local usedCurrency = playerMoney
				if buttonType == 'gems' then
					usedCurrency = playerGems
				elseif buttonType == 'matter' then
					usedCurrency = playerDarkMatter
				end

				local buttonCost = thisButton.cost.Value

				if usedCurrency.Value >= buttonCost and not tycoonObjectsContainer:FindFirstChild(thisButton.Name) then
					switchButton(thisButton, false)
					usedCurrency.Value -= buttonCost

					local object = referenceTycoonModel.objectsContainer:FindFirstChild(thisButton.Name)
					local objectClone = object:Clone()
					objectClone.Parent = tycoonObjectsContainer
					--animation where clone object being constructed part by part
					task.spawn(animation, objectClone, thisButton)

					updateButtons()
					local sndClone = buildSound:Clone()
					sndClone.Parent = thisButton
					sndClone:Play()
					task.wait(.6)
					sndClone:Destroy()

					if serverBuild == false then
						local documentOfPurchasing = Instance.new('Configuration')
						documentOfPurchasing.Parent = player[tycoonSaveName]
						documentOfPurchasing.Name = thisButton.Name
					end
				end
			end
		end
	end)
end
task.wait()

updateButtons()
ownerValue.Changed:Connect(function(plr)

	if not plr then
		clearObjects()
		updateButtons()
		switchButton(claimSpotButton, true)
	else
		switchButton(claimSpotButton, false)
		--Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ° ΡΠΎΡ…Ρ€Π°Π½ΠΈΠ²ΡˆΠΈΡ…ΡΡ ΠΏΠΎΠΊΡƒΠΏΠΎΠΊ
		local savedObjects = plr[tycoonSaveName]:GetChildren()
		if savedObjects ~= nil then
			for i,v in pairs(savedObjects) do
				local clone = referenceTycoonModel.objectsContainer:FindFirstChild(v.Name):Clone()
				clone.Parent = tycoonObjectsContainer
				task.wait()
			end
		end
		updateButtons()
	end
end)
claimSpotButton.ButtonBillboard.TextLabel.Text = 'Deploy a colony here for '..claimSpotButton.cost.Value .. "$"
if serverBuild == true then switchButton(claimSpotButton, false) end

claimSpotButton.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		if plr.leaderstats.money.Value >= claimSpotButton.cost.Value then
			plr.leaderstats.money.Value -= claimSpotButton.cost.Value
			switchButton(claimSpotButton, false)
			ownerValue.Value = plr
			script.claimed:FireClient(plr, tycoon.Parent)
		end
	end
end)

Is the assistent right? even if it is, why would this happen anyways???

1 Like

Privet

The problem is that the script doesnt handle multiple tycoons correctly.

Try changing code

--Unique id
local tycoonSaveName = "Tycoon_" .. tycoon.Name
local function initTycoonButtons()
    for i, button in pairs(referenceTycoonModel.buttonsContainer:GetChildren()) do
        --something
    end
end
initTycoonButtons()
ownerValue.Changed:Connect(function(newOwner)
    if newOwner then
        local saveFolder = newOwner:FindFirstChild(tycoonSaveName)
        if saveFolder then
            -- load
        end
    end
end)
local referenceTycoonModel = serverStorage:FindFirstChild(tycoon.referenceTycoonModel.Value)
if not referenceTycoonModel then
    warn("Reference model not found for tycoon: " .. tycoon.Name)
    return
end

And Debug

print("Initializing tycoon: " .. tycoon.Name)
print("Reference model: " .. tostring(referenceTycoonModel))
print("Owner value: " .. tostring(ownerValue.Value))

your ideas don’t give any information.

anyways, I forgot to rewrite some lones of code. this script was made to match old terrible system, where each plot had its build in workspace form the very start. this script was supposed to configure buttons INSIDE of the tycoon. then I made build global, one for many tycoons. and these scripts were trying to move buttons to their side which resulted in chaotic behaviour.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.