I’m currently working on my first tycoon. You have to buy droppers that drop money so you buy more and so on… the problem is that sometimes when I buy the dropper upgrade it starts to drop more than one block which it’s not supposed to do. I have tried using boolean values to check if the dropper has been activated already but it seems to still happen sometimes.
this is the script that is activated when the player walks on the upgrade and is prompted for the purchase. This code eventually fires a remote to the server.
dropper.Touched:Connect(function()
if touching_dropper == false then
touching_dropper = true
if dropper.active.Value == false then
--prompt confirm purchase ui
prompt_gui.Enabled = true
prompt_gui.Frame.TextLabel.Text = "Would you like to buy dropper for " .. dropper.price.Value .. "$ ?"
--connect functions for confiorm and cancel buttons
local confirm
confirm = prompt_gui.Frame.Frame.confirm.Activated:Connect(function()
--fire remote event to server
dropper_prompt:FireServer(dropper)
prompt_gui.Enabled = false
confirm:Disconnect()
end)
local cancel
cancel = prompt_gui.Frame.Frame.cancel.Activated:Connect(function()
prompt_gui.Enabled = false
cancel:Disconnect()
end)
--wait until the ui is closed or player walks away
repeat wait() until prompt_gui.Enabled == false or math.abs(player.Character.PrimaryPart.Position.X - dropper.Position.X) > 5 or math.abs(player.Character.PrimaryPart.Position.Z - dropper.Position.Z) > 5
prompt_gui.Enabled = false
end
task.wait(0.5)
touching_dropper = false
end
end)
this is the server script that fires when it receives the remote
dropper_prompt.OnServerEvent:Connect(function(player, dropper)
local slots_store = DataStore2("slots", player)
local coin_store = DataStore2("coins", player)
local orb_store = DataStore2("orb", player)
local money_store = DataStore2("money", player)
--check if price is correct and deduct form current money amount
if dropper.price.Value <= money_store:Get() then
dropper.active.Value = true
money_store:Increment(-dropper.price.Value)
dropper.Parent.fade.Color = Color3.new(0,1,0)
dropper.model.Value.Transparency = 0
--spawn function that continuously spawns orbs from the dropper
spawn(function()
while dropper.active.Value == true do
local block = orb_store:Get():Clone()
block.Position = dropper.model.Value.Position + Vector3.new(-2.1,-2.3,0)
block.Parent = dropper.model.Value
block.Anchored = false
game.Debris:AddItem(block, 20)
block.Touched:Connect(function(part)
--add money when orb touches the collector
if part == slots_store:Get().maindropper.collector then
coin_store:Increment(block.multiplier.Value)
slots_store:Get().maindropper.screen.SurfaceGui.TextLabel.Text = coin_store:Get()
local orb_size = slots_store:Get().maindropper.dropper_orb.orb.Size
local multiplier = orb_store:Get().multiplier.Value
if orb_size.x < 8 then
slots_store:Get().maindropper.dropper_orb.orb.Size += Vector3.new(0.1/multiplier,0.1/multiplier,0.1/multiplier)
slots_store:Get().maindropper.dropper_orb.orb.Fire.Size += 0.4/multiplier
else
slots_store:Get().maindropper.dropper_orb.orb.Fire.Heat += 1/multiplier
end
block:Destroy()
end
end)
task.wait(1)
end
end)
else
print("price is: ", dropper.price.Value)
end
end)
I’m very happy to receive possible solutions. (I’m also happy to receive feedback on the code if I have any bad practices as I’m quite new to coding)