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

Problem with my PurchaseHandler script?

In my tycoon, I want it to show a gui that says you don't have enough cash when you touch a button that costs more than you have. But, my code doesn't error, nor does it work. Help?

My problem is at line 96, if the code is too long for you.

objects = {}

teamcolor = BrickColor.new(script.Parent.Name)
config = script.Parent.Parent.Parent.Configuration

wait(1)

script.Parent.Essentials.Spawn.TeamColor = teamcolor
script.Parent.Essentials.Spawn.BrickColor = teamcolor

script.Parent.Essentials.Collector.Touched:connect(function(hit)
	if hit:FindFirstChild("Cash") then
		script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value
		Instance.new("Sparkles", hit).Color = Color3.new(math.random(1, 255) / 255, math.random(1, 255) / 255, math.random(1, 255) / 255)
		game.Debris:AddItem(hit, 0.1)
	end
end)

script.Parent.Essentials.Giver.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player ~= nil then
		if script.Parent.Owner.Value == player then
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Humanoid.Health > 0 then
					local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
					
					if cashmoney ~= nil then 
						cashmoney.Value = cashmoney.Value + script.Parent.Cash.Value
						script.Parent.Cash.Value = 0
					end
				end
			end
		end
	end
end)

script.Parent:WaitForChild("Buttons")

for i, v in pairs(script.Parent.Buttons:GetChildren()) do
	if v:FindFirstChild("Head") then
		local object = script.Parent.Purchases:FindFirstChild(v.Object.Value)
		
		if object ~= nil then
			objects[object.Name] = object:Clone()
			object:Destroy()
		else
			print("Button: " .. v.Name .. " is missing its object and has been removed.")
			v.Head.CanCollide = false
			v.Head.Transparency = 1
		end
								
		if v:FindFirstChild("Dependency") then
			v.Head.CanCollide = false
			v.Head.Transparency = 1
			
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedObjects:WaitForChild(v.Dependency.Value) then
					if config.ButtonFadeInDependency.Value == true then
						for i = 1, 20 do
							wait(config.ButtonFadeInTime.Value / 20)
							v.Head.Transparency = v.Head.Transparency - 0.05
						end
					end
					v.Head.CanCollide = true
					v.Head.Transparency = 0
				end
			end))
		end
		
		v.Head.Touched:connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			if v.Head.CanCollide == true then
				if player ~= nil then
					if script.Parent.Owner.Value == player then
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent.Humanoid.Health > 0 then
								local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
								
								if cashmoney ~= nil then
									if cashmoney.Value >= v.Price.Value then
										cashmoney.Value = cashmoney.Value - v.Price.Value
										objects[v.Object.Value].Parent = script.Parent.PurchasedObjects
										
										if config.ButtonExplodeOnBuy.Value == true then
											local explosion = Instance.new("Explosion", workspace)
											explosion.Position = v.Head.Position
											explosion.DestroyJointRadiusPercent = 0
											explosion.BlastPressure = 0
										end
										
										if config.ButtonFadeOutOnBuy.Value == true then
											v.Head.CanCollide = false
											coroutine.resume(coroutine.create(function()
												for i = 1, 20 do
													wait(config.ButtonFadeOutTime.Value / 20)
													v.Head.Transparency = v.Head.Transparency + 0.05
												end
											end))
										else
											v.Head.CanCollide = false
											v.Head.Transparency = 1
										end
									end
								end
							end
						end
					end
				end
			end
		end)
	end
end

1 answer

EDIT Heres what you might want to put on line 74.

local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(cashmoney.Value)
--cashmoney.Value can be whatever your cashmoney tag's name is, but it must have .Value

Also, on line 96, your script should say this:

else cashmoney < v.Price.Value then
--you got your signs mixed around. Just pretend the less than or greater than signs are alligators and want to eat the larger one. :D

1 comment

0

And one line 23 change the local cashmoney code to the same for line 74.

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