Hi Developers!
So I have a local script which creates buttons, and when you press them it fires a ServerEvent
. It needs to fire one time, but for some reason it fires 7 TIMES
Here’s the code:
Client
newTypeButton.MouseButton1Click:Connect(function()
for i, child in pairs(loadout.WeaponsList:GetChildren()) do
if child:IsA("TextButton") then child:Destroy() end
end
for i, weaponChild in ipairs(weaponType:GetChildren()) do
local newWeaponButton = script.WeaponButton:Clone()
newWeaponButton.Text = weaponChild.Name
newWeaponButton.Name = weaponChild.Name
newWeaponButton.Parent = loadout.WeaponsList
print(weaponChild, weaponChild.ClassName)
local statgui = frame.Parent.StatsFrame
local UnlockedStatus = getunlockedEvent:InvokeServer(weaponChild.Name)
if UnlockedStatus == nil then
UnlockedStatus = false
end
print(UnlockedStatus, weaponChild.Name)
newWeaponButton.MouseButton1Click:Connect(function()
local SettingModule = require(weaponChild:WaitForChild("Setting"))
local StatsModule = require(weaponChild:WaitForChild("Setting"):WaitForChild("1"))
local Price = getpriceEvent:InvokeServer(weaponChild.Name)
print(Price, "price for", weaponChild.Name, UnlockedStatus)
end)
if UnlockedStatus then
loadout.WeaponsList[weaponChild.Name].LockedUI.Visible = false
end
--update UI if weapon has been bought
rs.WeaponSystemEvents.UpdateWeapon.OnClientEvent:Connect(function()
print("updated UI")
end)
--==================buy and equip events fire===================== HERE IS THE PROBLEM
local equipEvent = rs.WeaponSystemEvents.Equip --event with the problem
loadout.BuyDescFrame.EquipButton.MouseButton1Click:Connect(function() equipEvent:FireServer(weaponChild.Name) updateLoadout() end) --firing this event
end
end)
Server
------prices------
local weaponPrices = {
--historical
["G36"] = {
name = "G36",
price = 0,
class = "Primary",
},
["Five-Seven"] = {
name = "Five-Seven",
price = 0,
class = "Secondary",
},
["Glock 17"] = {
name = "Glock 17",
price = 9000,
class = "Secondary",
},
["SKS"] = {
name = "SKS",
price = 3300,
class = "Primary",
},
["Remington 870"] = {
name = "Remington 870",
price = 7200,
class = "Primary",
},
--unnatural
["Fire"] = {
name = "Fire",
price = 1800,
class = "Secondary",
},
["Magic Stick"] = {
name = "Magic Stick",
price = 3000,
class = "Primary",
},
}
------------------
--[[weaponTable is the data, here is the default structure
local weaponTable = {
OwnedWeapons = {
["G36"] = true
},
EquippedWeapons = {
Primary = nil,
Secondary = nil,
Melee = nil
},
}
]]
--=====================Equipping and stuff==========================
local equipEvent = rs.WeaponSystemEvents:WaitForChild("Equip")
equipEvent.OnServerEvent:Connect(function(player, wpn)
warn("fired!", wpn)
for key, info in pairs(weaponPrices) do
if wpn == info.name then
AddToEquipped(wpn, info.class)
print("equipped", wpn, "and inserted to", weaponTable.EquippedWeapons)
else
warn("no such a weapon!")
end
end
end)
I’d appreciate any help provided! And if I missed some information. please let me know! Also I don’t mind some additional help for scripts themselves!