I’m practicing my scripting skills with remote events and creating a touch event that triggers a shop to appear. The problem is when I don’t have enough money to buy the item in the shop, I can’t make the shop gui disappear once I try to buy it with the incorrect amount of money. I swapped the gui as parameters and tried using debounce to align or correct the issue. I think the debounce did work but I don’t think that’s the proper way to fix this issue.
Local script(buying local script):
local shopgui = script.Parent.ShopGui
local exitgui = script.Parent.ExitGui
local button = shopgui.Frame.Buy
local value = button.Value
local player = game.Players.LocalPlayer
local money = player.leaderstats.Money
button.MouseButton1Click:Connect(function()
game.ReplicatedStorage.MoneyEvent:FireServer(value.Value, tonumber(money.Value), shopgui, exitgui)
end)
game.ReplicatedStorage.TouchEvent.OnClientEvent:Connect(function()
script.Parent.ShopGui.Enabled = true
end)
leaderstats and events for shop:
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local money = Instance.new("IntValue", folder)
money.Name = "Money"
end)
game.ReplicatedStorage.MoneyEvent.OnServerEvent:Connect(function(player, cost, amount, shop, exit)
print(cost)
print(amount)
print(shop)
if cost > amount then
shop.Enabled = false
exit.Enabled = true
task.wait(2)
exit.Enabled = false
shop.Enabled = true
elseif cost <= amount then
local newamount = amount - cost
player.leaderstats.Money.Value = newamount
end
end)
game.Workspace.Part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.TouchEvent:FireClient(player)
end)
well you’re going into the playergui and getting the gui and unenabling it for one and opposite for the other and then in 2 seconds your doing the opposite.
yes it is, debounding is extremely common in .touch script due to players moving so much and triggering it every second
db = false
game.Workspace.Part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not db then
db = true
game.ReplicatedStorage.TouchEvent:FireClient(player)
wait(1)
db = false
end
end)
The problem is once a player has touched it the first time, he/she has already made the enabled = false and enabled = true clash together so that it wouldn’t work.