Need help with tycoon bug

I have been working on a tycoon for a few months and earlier while I was testing it in game I noticed that the buttons which you stand on to buy things were deducting two or three times the price of the item from the player instead of the original price. For example, if the item was $100 it was deducting $300 instead.

Here is the main server script:

local module = require(script.Parent.Settings)
local plrCash = Instance.new(‘Folder’, game.ServerStorage)
local clones = Instance.new(‘Folder’, game.ServerStorage)
local parts = Instance.new(‘Folder’, workspace)
plrCash.Name = ‘PlayerCash’
parts.Name = ‘Parts’
clones.Name = ‘TycoonClones’

if module.autoAssignTeams == false then
local neutralTeam = Instance.new(‘Team’, game:GetService(‘Teams’))
neutralTeam.Name = module.neutralTeamName
neutralTeam.AutoAssignable = true
end

for i,tycoon in pairs(script.Parent.Tycoons:GetChildren()) do
tycoon:Clone().Parent = clones
local team = Instance.new(‘Team’, game:GetService(‘Teams’))
team.TeamColor = tycoon.TycoonInfo.TeamColor.Value
team.Name = tycoon.Name
team.AutoAssignable = module.autoAssignTeams
tycoon.Essentials.Spawn.TeamColor = tycoon.TycoonInfo.TeamColor.Value
tycoon.Essentials.Spawn.BrickColor = tycoon.TycoonInfo.TeamColor.Value
tycoon.Main.Disabled = false
end
game.Players.PlayerAdded:Connect(function(plr)
local dss = game:GetService(“DataStoreService”)
local ds2 = dss:GetDataStore(“Customers”)
local Money = Instance.new(‘Folder’, plr)
Money.Name = ‘Money’
local cash = Instance.new(‘IntValue’, Money)
cash.Name = module.currencyName

local numValue = Instance.new(‘NumberValue’, plrCash)
numValue.Name = plr.Name
local objValue = Instance.new(‘ObjectValue’, numValue)
objValue.Name = ‘OwnsTycoon’

if module.autoAssignTeams then
plr.Team.AutoAssignable = false
local tycoon = script.Parent.Tycoons:FindFirstChild(plr.Team.Name)
if tycoon then
for i, obj in ipairs(tycoon:GetDescendants()) do
if obj.Name == “GateControl” and obj:IsA(“Script”) then
obj.ClaimTycoon:Fire(plr)
end
end
end
end

while wait() do
cash.Value = numValue.Value

end
end)

Part of script that deducts the money from a player after they buy the item:

if object then
button:WaitForChild(“Head”).Touched:Connect(function(hit)
local plr = game.Players:FindFirstChild(hit.Parent.Name)
if plr and tcnInfo.Owner.Value == plr and button.Head.Transparency == 0 then
if button:FindFirstChild(‘Price’) then
local cash = game.ServerStorage:WaitForChild(‘PlayerCash’):FindFirstChild(plr.Name)
if cash and cash.Value >= button.Price.Value then
cash.Value -= button.Price.Value
purchaseObject()
elseif cash and cash.Value < button.Price.Value then
playSound(button.Head, module.errorBuySound)
end

I reverted the game to one of the first versions and the issue was even present then when it wasn’t previously, so I don’t know if this is a Roblox issue or just a script error.

Any help would be appreciated.

Does it take more money the more you stay on the button?

Sometimes it works normally so I don’t think so

If i were you, I would just create a debouce so that way the touch event doesn’t run multiple times before the 1st time finishes

1 Like

Ok I’ll try that and see if it fixes the issue, ty.

Please format your code correctly next time so that it’s readable…