Hi Other Developers,
I have this big problem where I press a button that fires a RemoteEvent, but that RemoteEvent takes away more than its supposed to. There is a reason for how much it takes away.
For example, I press on Button2 which takes away 50, but it also takes away from Button3, so it takes away from two buttons instead of one. So if there are two buttons equal to 50 dollars and I buy one, it minuses 100 instead.
Here is my LocalScriptCode:
(IT IS NOT ALL OF IT)
--// EVENTS
local RemoteEventsFolder = game.ReplicatedStorage:WaitForChild("RemoteEvents")
local AddCashEvent = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " AddCashEvent")
--// REBIRTH EVENTS
local RebirthEvent1 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " RebirthEvent1")
local RebirthEvent2 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " RebirthEvent2")
local RebirthEvent3 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " RebirthEvent3")
local RebirthEvent4 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " RebirthEvent4")
-- MINUS CASH EVENTS
local MinusCashEvent1 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " MinusCashEvent1")
local MinusCashEvent2 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " MinusCashEvent2")
local MinusCashEvent3 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " MinusCashEvent3")
local MinusCashEvent4 = RemoteEventsFolder:WaitForChild(game.Players.LocalPlayer.Name .. " MinusCashEvent4")
spawn(function()
for _, ClickForRebirthButton in pairs(RebirthShop:WaitForChild("ScrollFrame"):GetChildren()) do
if ClickForRebirthButton:IsA("TextButton") then
if ShopItemsModule[ClickForRebirthButton.Name] ~= nil then
print(ClickForRebirthButton.Name)
ShopItemPrice = ShopItemsModule[ClickForRebirthButton.Name].Price
print(ShopItemPrice)
ClickForRebirthButton.Text = "$" .. ShopItemPrice
ClickForRebirthButton.Activated:Connect(function()
print("ButtonID: " .. ClickForRebirthButton.Name .. " / Price: " .. tostring(ShopItemPrice))
if Cash.Value >= tonumber(string.match(ClickForRebirthButton.Text, "%d+")) then
if tonumber(string.match(ClickForRebirthButton.Text, "%d+")) == ShopItemsModule["Rebirth 1"].Price then
MinusCashEvent1:FireServer()
RebirthEvent1:FireServer()
print(MinusCashEvent1.Name .. "Fired")
return
elseif tonumber(string.match(ClickForRebirthButton.Text, "%d+")) == ShopItemsModule["Rebirth 2"].Price then
MinusCashEvent2:FireServer()
RebirthEvent2:FireServer()
print(MinusCashEvent2.Name .. "Fired")
return
elseif tonumber(string.match(ClickForRebirthButton.Text, "%d+")) == ShopItemsModule["Rebirth 3"].Price then
MinusCashEvent3:FireServer()
RebirthEvent3:FireServer()
print(MinusCashEvent3.Name .. "Fired")
return
elseif tonumber(string.match(ClickForRebirthButton.Text, "%d+")) == ShopItemsModule["Rebirth 4"].Price then
MinusCashEvent4:FireServer()
RebirthEvent3:FireServer()
print(MinusCashEvent4.Name .. "Fired")
return
end
CashGUI.Text = "$" .. tostring(Cash.Value)
end
end)
end
end
end
end)
Very Not Optimized but I’m farily new to coding so forgive me.
I also have the ModuleScript.
local shop = {
["Rebirth 1"] = {
Price = 25,
AddRebirthCount = 1,
},
["Rebirth 2"] = {
Price = 50,
AddRebirthCount = 2,
},
["Rebirth 3"] = {
Price = 100,
AddRebirthCount = 4,
},
["Rebirth 4"] = {
Price = 200,
AddRebirthCount = 8,
},
}
return shop
-- farily simple
And lastly my ServerScript which recieves the RemoteEvents:
-- still not optimized xd
game.Players.PlayerAdded:Connect(function(plr)
--// Variables
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local RemoteEventsFolder = Instance.new("Folder")
RemoteEventsFolder.Name = "RemoteEvents"
RemoteEventsFolder.Parent = game.ReplicatedStorage
--// When ClickButton is Clicked Will add Cash into Leaderstats
local AddCashEvent = Instance.new("RemoteEvent")
AddCashEvent.Name = plr.Name .. " AddCashEvent"
AddCashEvent.Parent = RemoteEventsFolder
local function AddCashFunction()
Cash.Value += (1 + plr.leaderstats.Rebirths.Value)
end
AddCashEvent.OnServerEvent:Connect(AddCashFunction)
local RebirthButtonModule = require(game.ReplicatedStorage.RebirthShopProperties)
--// Take Away Cash Event
local MinusCashEvent1 = Instance.new("RemoteEvent")
MinusCashEvent1.Name = plr.Name .. " MinusCashEvent1"
MinusCashEvent1.Parent = RemoteEventsFolder
local function MinusCashFunction1()
Cash.Value -= RebirthButtonModule["Rebirth 1"].Price
end
MinusCashEvent1.OnServerEvent:Connect(MinusCashFunction1)
--// Take Away Cash (Second One)
local MinusCashEvent2 = Instance.new("RemoteEvent")
MinusCashEvent2.Name = plr.Name .. " MinusCashEvent2"
MinusCashEvent2.Parent = RemoteEventsFolder
local function MinusCashFunction2()
Cash.Value -= RebirthButtonModule["Rebirth 2"].Price
print(RebirthButtonModule["Rebirth 2"].Price)
end
MinusCashEvent2.OnServerEvent:Connect(MinusCashFunction2)
--// Take Away Cash (Third One)
local MinusCashEvent3 = Instance.new("RemoteEvent")
MinusCashEvent3.Name = plr.Name .. " MinusCashEvent3"
MinusCashEvent3.Parent = RemoteEventsFolder
local function MinusCashFunction3()
Cash.Value -= RebirthButtonModule["Rebirth 3"].Price
end
MinusCashEvent2.OnServerEvent:Connect(MinusCashFunction3)
--// Take Away Cash (Fourth One)
local MinusCashEvent3 = Instance.new("RemoteEvent")
MinusCashEvent3.Name = plr.Name .. " MinusCashEvent4"
MinusCashEvent3.Parent = RemoteEventsFolder
local function MinusCashFunction4()
Cash.Value -= RebirthButtonModule["Rebirth 4"].Price
end
MinusCashEvent2.OnServerEvent:Connect(MinusCashFunction4)
----// REBIRTH EVENTS BELOW
--// REBIRTH 1
local RebirthEvent = Instance.new("RemoteEvent")
RebirthEvent.Name = plr.Name .. " RebirthEvent1"
RebirthEvent.Parent = RemoteEventsFolder
local function AddRebirth()
Rebirths.Value += RebirthButtonModule["Rebirth 1"].AddRebirthCount
end
RebirthEvent.OnServerEvent:Connect(AddRebirth)
--// REBIRTH 2
local RebirthEvent = Instance.new("RemoteEvent")
RebirthEvent.Name = plr.Name .. " RebirthEvent2"
RebirthEvent.Parent = RemoteEventsFolder
local function AddRebirth2()
Rebirths.Value += RebirthButtonModule["Rebirth 2"].AddRebirthCount
end
RebirthEvent.OnServerEvent:Connect(AddRebirth2)
--// REBIRTH 3
local RebirthEvent = Instance.new("RemoteEvent")
RebirthEvent.Name = plr.Name .. " RebirthEvent3"
RebirthEvent.Parent = RemoteEventsFolder
local function AddRebirth3()
Rebirths.Value += RebirthButtonModule["Rebirth 3"].AddRebirthCount
end
RebirthEvent.OnServerEvent:Connect(AddRebirth3)
--// REBIRTH 4
local RebirthEvent = Instance.new("RemoteEvent")
RebirthEvent.Name = plr.Name .. " RebirthEvent4"
RebirthEvent.Parent = RemoteEventsFolder
local function AddRebirth4()
Rebirths.Value += RebirthButtonModule["Rebirth 4"].AddRebirthCount
end
RebirthEvent.OnServerEvent:Connect(AddRebirth4)
end)
Thanks a lot!
![]()