I was trying to fix a bug where the shop takes more currency then needed
Script that worked but toke more currency then needed
local BuyItemEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("ItemPurchased")
local BuyPerkEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("PerkPurchased")
local PerkHandler = require(script.PerkHandler)
BuyPerkEvent.OnServerEvent:Connect(function(Player,Perk,CostOfItem)
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedPerk.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.PerkThatWasPurchased.Value = Perk.Value
if Perk.Value ~= "Stamina Upgrade" then
if Perk.Value == "Health Upgrade" then
PerkHandler.Health_Upgrade(Player)
end
end
end
end)
BuyItemEvent.OnServerEvent:Connect(function(Player,Item,CostOfItem)
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedItem.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.ItemThatWasPurchased.Value = Item.Value
end
end)
New script that doesn’t work no more
local BuyItemEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("ItemPurchased")
local BuyPerkEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("PerkPurchased")
local PerkHandler = require(script.PerkHandler)
local PurchaseConnection1 = BuyPerkEvent.OnServerEvent:Connect(function(Player,Perk,CostOfItem)
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedPerk.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.PerkThatWasPurchased.Value = Perk.Value
if Perk.Value ~= "Stamina Upgrade" then
if Perk.Value == "Health Upgrade" then
PerkHandler.Health_Upgrade(Player)
end
end
end
end)
task.wait(1)
PurchaseConnection1:Disconnect()
local PurchaseConnection2 = BuyItemEvent.OnServerEvent:Connect(function(Player,Item,CostOfItem)
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedItem.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.ItemThatWasPurchased.Value = Item.Value
end
end)
task.wait(1)
PurchaseConnection2:Disconnect()
The problem with the new script is when you try to purchase something it comes out not enough money even when there is enough
1 Like
Try using this to subtract the currency:
Player.leaderstats.Coins.Value -= CostOfItem.Value
im pretty sure that makes no difference though besides making it shorter and pretty. youd think even …
Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - CostOfItem.Value
… would at least guarantee the original value is being overwritten.
to stickz: i would suggest using some print() debugging at the beginning of your code to make sure the values that are being put in are the correct values. because otherwise, the code looks perfectly normal to me.
for example, after the :Connect line, i would put a:
print("Perk/Item: " .. tostring(Perk.Value)) -- or Item.Value, depending on the function
print("Item Cost: " .. tostring(CostOfItem.Value))
perhaps itll give you a better clue to whats up, and if something outside of this code is a part of the issue?
Maybe this?
BuyButtion.MouseButton1Click:Connect(function()
if IsAItemSelectedValue.Value == true and PurchasedItem.Value == false and ButtionCooldown == false and PressedButtion:GetAttribute("TypeofButtion") == "Item" then
ButtionCooldown = true
BuyItemEvent:FireServer(PressedButtion.ItemThatTheButtionsells,PressedButtion.Cost)
task.wait(0.2)
if PurchasedItem.Value == true then
BuyButtion.Text = "Item Purchased!"
task.wait(2)
BuyButtion.Text = "You already purchased a item you can't purchase another one!"
else
ButtionCooldown = true
BuyButtion.Text = "Not Enough BaldiCoins!"
task.wait(2)
BuyButtion.Text = "Buy item"
ButtionCooldown = false
end
task.wait(1)
ButtionCooldown = false
elseif IsAItemSelectedValue.Value == true and PurchasedPerk.Value == false and ButtionCooldown == false and PressedButtion:GetAttribute("TypeofButtion") == "Perk" then
ButtionCooldown = true
BuyPerkEvent:FireServer(PressedButtion.PerkThatTheButtionsells,PressedButtion.Cost)
task.wait(0.2)
if PurchasedPerk.Value == true then
BuyButtion.Text = "Perk Purchased!"
task.wait(2)
BuyButtion.Text = "You already purchased a Perk you can't purchase another one!"
else
ButtionCooldown = true
BuyButtion.Text = "Not Enough BaldiCoins!"
task.wait(2)
BuyButtion.Text = "Buy Perk"
ButtionCooldown = false
end
task.wait(1)
ButtionCooldown = false
end
end)
There is ButtionCooldown thing but maybe it doesn’t work right?
Yeah, i know and i agree with you but i find that its much more reliable to use -= and ive had problems with using that other method in the past. So, i was just suggesting a possible solution.
1 Like
Used this it didn’t solve the issue
oh its alright, im not very familiar with what exactly works the best in lua since im more experienced in other languages like javascript, haha. ive had moments like that in lua too so i agree its a good shot ;D
1 Like
Hmmm, like @samlovesmath said, you should probably try debugging it.
Also maybe try testing it while using a variable for CostOfItem instead of firing it through a remote event to test if you still have the same issue no matter what
1 Like
Doing that now
(30 words requirement)
1 Like
Did the debugging this is what the output printed when I clicked the buybuttion once
Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.384 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.384 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.384 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.384 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.384 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.385 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.385 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.385 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.386 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.386 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.386 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.386 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.386 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.386 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.387 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.387 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.387 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.387 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.387 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.387 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.388 Item Cost: 100 - Server - ShopScript (ServerSide):23
12:11:59.388 Perk/Item: Quarter - Server - ShopScript (ServerSide):22
12:11:59.388 Item Cost: 100 - Server - ShopScript (ServerSide):23
Decided to buy a perk next this is what the output printed
Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.134 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.134 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.135 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.135 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.135 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.136 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.136 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.136 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.136 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.137 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.137 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.137 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.137 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.138 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.138 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.138 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.138 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.139 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.139 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.139 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.140 Item Cost: 250 - Server - ShopScript (ServerSide):7
12:13:44.140 Perk/Item: Health Upgrade - Server - ShopScript (ServerSide):6
12:13:44.140 Item Cost: 250 - Server - ShopScript (ServerSide):7
Okay now that I debugged I think this might be the problem in the client side script
BuyButtion.MouseButton1Click:Connect(function()
if IsAItemSelectedValue.Value == true and PurchasedItem.Value == false and ButtionCooldown == false and PressedButtion:GetAttribute("TypeofButtion") == "Item" then
ButtionCooldown = true
BuyItemEvent:FireServer(PressedButtion.ItemThatTheButtionsells,PressedButtion.Cost)
task.wait(0.2)
if PurchasedItem.Value == true then
BuyButtion.Text = "Item Purchased!"
task.wait(2)
BuyButtion.Text = "You already purchased a item you can't purchase another one!"
else
ButtionCooldown = true
BuyButtion.Text = "Not Enough BaldiCoins!"
task.wait(2)
BuyButtion.Text = "Buy item"
ButtionCooldown = false
end
task.wait(1)
ButtionCooldown = false
elseif IsAItemSelectedValue.Value == true and PurchasedPerk.Value == false and ButtionCooldown == false and PressedButtion:GetAttribute("TypeofButtion") == "Perk" then
ButtionCooldown = true
BuyPerkEvent:FireServer(PressedButtion.PerkThatTheButtionsells,PressedButtion.Cost)
task.wait(0.2)
if PurchasedPerk.Value == true then
BuyButtion.Text = "Perk Purchased!"
task.wait(2)
BuyButtion.Text = "You already purchased a Perk you can't purchase another one!"
else
ButtionCooldown = true
BuyButtion.Text = "Not Enough BaldiCoins!"
task.wait(2)
BuyButtion.Text = "Buy Perk"
ButtionCooldown = false
end
task.wait(1)
ButtionCooldown = false
end
end)
So, is the item cost correct? Also its strange that it prints so much for one button click
The item and the cost is 100& correct
1 Like
Okay, quick suggestion, you should never send a price through the client. Its just not a good practice. I dont know if this would be causing the issue, but you shouldnt do it. Rather do it using a variable in the serverscript or create an intvalue/numbervalue and reference that.
If none of that works ill read the your code more and try to help the best i can
The price is a IntValue
(30 words requirement)
I did more debugging I figured out that it was the buybuttion I did a print yes for when I clicked the buybuttion it came out 12 times over 1 click
1 Like
you can actually accidentally create duplicate events which get applied over and over again. for example:
- connecting an event through a script, and having that script run multiple times.
- having duplicates of a script (which means you have an event for every duplicated script)
- not properly disconnecting events when needed
the duplicated events can be one cause. but im looking over your past code and something odd caught my eye, about the 3rd point i just noted.
for your event lines in the first post, you have your PurchaseConnections
and then the Disconnect
for it at the bottom. im not sure why you created the event and then had it disconnected only 1 second later. wouldnt you put that inside of the event instead, so that as soon as its completed once, it disconnects itself? (or you could have it both in there, and on the outside as a “timeout” catch)
if the server sends multiple fires before that task.wait
ends, the event will repeat itself until the task.wait
is over, and the event disconnects. that might mean youre technically purchasing the item more than once.
Did the :Disconnect() thing the event only fired once ever when I clicked the buybuttion again it didn’t fire again
This is the new code
local BuyItemEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("ItemPurchased")
local BuyPerkEvent = game.ReplicatedStorage.ShopEvents:WaitForChild("PerkPurchased")
local PerkHandler = require(script.PerkHandler)
local PurchaseConnection2
local PurchaseConnection1
PurchaseConnection1 = BuyPerkEvent.OnServerEvent:Connect(function(Player,Perk,CostOfItem)
print("yes2")
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedPerk.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.PerkThatWasPurchased.Value = Perk.Value
if Perk.Value ~= "Stamina Upgrade" then
if Perk.Value == "Health Upgrade" then
PerkHandler.Health_Upgrade(Player)
end
end
end
PurchaseConnection1:Disconnect()
end)
PurchaseConnection2 = BuyItemEvent.OnServerEvent:Connect(function(Player,Item,CostOfItem)
print("yes1")
if Player.leaderstats.BaldiCoins.Value >= CostOfItem.Value then
Player.PurchasedItem.Value = true
Player.leaderstats.BaldiCoins.Value = Player.leaderstats.BaldiCoins.Value - CostOfItem.Value
Player.ItemThatWasPurchased.Value = Item.Value
end
PurchaseConnection2:Disconnect()
end)