Tycoon script not working

local lemonadestand = ScrollingFrame.LemonadeStand
local lsAmount = 20
local perspawn = 2
local dollar
local lsHasManager = false
local lsPurchaseManager = script.Parent.Parent:WaitForChild("ManagerShop").ScrollingFrame.Frame.LSManager.TextButton
lsPurchaseManager.MouseButton1Click:Connect(function()
	if plr.leaderstats.Cash.Value >= 200 then
		lsHasManager = true
		plr.leaderstats.Cash.Value -= 200
		lsPurchaseManager.Text = "You already own this."
		lsPurchaseManager.Active = false
	else
		StarterGui:SetCore("SendNotification", {
			Title = "Insufficient funds.",
			Text = "You don't have enough cash to purchase this item."
		})
	end
	lsHasManager = true 
end)
if lsHasManager == false then
	lemonadestand.TextButton.MouseButton1Click:Connect(function()
		dollar = workspace.LemonadeStandSpawner:Clone()
		dollar.Parent = workspace
		dollar.Position = workspace.LemonadeStandSpawner.Position - Vector3.new(0,1,0)
		dollar.Anchored = false
		dollar.CanCollide = true
	end)
elseif lsHasManager == true then
	task.spawn(function()
		while task.wait(1) do
			dollar = workspace.LemonadeStandSpawner:Clone()
			dollar.Parent = workspace
			dollar.Position = workspace.LemonadeStandSpawner.Position - Vector3.new(0,1,0)
			dollar.Anchored = false
			dollar.CanCollide = true
		end
	end)
end

when a player clicks on lsPurchaseManager and they have sufficient funds, i want the code in the task.spawn(function() to work, but it doesn’t. how do i fix this

2 Likes

It seems like you are trying to spawn lemonade stands when a player clicks on lsPurchaseManager and if they have the manager purchased. The issue in your code is that the lsHasManager variable is being checked immediately after the player clicks, and its value will be false at that point because the event has not had a chance to execute yet. Additionally, there’s an issue with the if and elseif statements, and you might not be entering the correct branch to spawn the lemonade stands.

To fix this, you can move the spawning logic inside the MouseButton1Click event handler for lsPurchaseManager and remove the if lsHasManager == false and elseif lsHasManager == true conditions. Instead, you can directly use the value of lsHasManager to determine whether to spawn the lemonade stands. Here’s an example:

local lemonadestand = ScrollingFrame.LemonadeStand
local lsAmount = 20
local perspawn = 2
local dollar
local lsHasManager = false
local lsPurchaseManager = script.Parent.Parent:WaitForChild("ManagerShop").ScrollingFrame.Frame.LSManager.TextButton

lsPurchaseManager.MouseButton1Click:Connect(function()
	local plr = game.Players.LocalPlayer
	if plr.leaderstats.Cash.Value >= 200 then
		lsHasManager = true
		plr.leaderstats.Cash.Value -= 200
		lsPurchaseManager.Text = "You already own this."
		lsPurchaseManager.Active = false
		-- You can directly spawn the lemonade stands here as the player just purchased the manager.
		task.spawn(function()
			while task.wait(1) do
				dollar = workspace.LemonadeStandSpawner:Clone()
				dollar.Parent = workspace
				dollar.Position = workspace.LemonadeStandSpawner.Position - Vector3.new(0, 1, 0)
				dollar.Anchored = false
				dollar.CanCollide = true
			end
		end)
	else
		StarterGui:SetCore("SendNotification", {
			Title = "Insufficient funds.",
			Text = "You don't have enough cash to purchase this item."
		})
	end
end)

Now, supposedly the lemonade stands should spawn immediately after the player purchases the manager, and you no longer need to do the separate lsHasManager check, that’s it.

2 Likes

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