Hi Developers! ![]()
So I’ve been making weapon system for my game, when this big issue appeared: when client interacts with buttons, the event is fired not only for the player, but ALL clients present on the server. Does it have to do something with the for loop? And how to fix this?
I’d appreciate sany help provided. And if I need to clarify some info, let me know! ![]()
Local script located in StarterGui
I don’t know what exact piece of code to put here…
local tweenService = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local getunlockedEvent = rs.WeaponSystemEvents:WaitForChild("GetUnlockStatus")
local getpriceEvent = rs.WeaponSystemEvents:WaitForChild("GetPrice")
local plr = game.Players.LocalPlayer
local weaponsFolder = rs:WaitForChild("WeaponStorage")
local weaponClasses = {weaponsFolder.Historical, weaponsFolder.Unnatural}
local weaponTypes = {weaponsFolder.Historical:GetChildren(), weaponsFolder.Unnatural:GetChildren()}
local frame = script.Parent
local loadout = frame:WaitForChild("Loadout")
local ActualUI = script.Parent.Parent.Parent.Parent.Parent:WaitForChild("ActualUI")
local currentMultiplierTable = {}
local function updateLoadout(primary, secondary, melee)
print(primary, secondary, melee)
frame.Parent.EquippedWeapons.WeaponsEquipped.Text = ""
if primary ~= nil then
frame.Parent.EquippedWeapons.WeaponsEquipped.Text = "Primary: " .. primary
end
if secondary ~= nil then
frame.Parent.EquippedWeapons.WeaponsEquipped.Text = frame.Parent.EquippedWeapons.WeaponsEquipped.Text .. "\nSecondary: " .. secondary
end
if melee ~= nil then
frame.Parent.EquippedWeapons.WeaponsEquipped.Text = frame.Parent.EquippedWeapons.WeaponsEquipped.Text .. "\nMelee: " .. melee
end
end
updateLoadout(" ", " ", " ")
for i, weaponClass in ipairs(weaponClasses) do
local newButton = script.WeaponClassButton:Clone()
newButton.Text = weaponClass.Name
newButton.Parent = loadout.WeaponClasses
newButton.MouseButton1Click:Connect(function()
for i, child in pairs(loadout.TypesList:GetChildren()) do
if child:IsA("TextButton") then child:Destroy() end
end
for i, child in pairs(loadout.WeaponsList:GetChildren()) do
if child:IsA("TextButton") then child:Destroy() end
end
for i, weaponType in ipairs(weaponClass:GetChildren()) do
local newTypeButton = script.WeaponTypeButton:Clone()
newTypeButton.Text = weaponType.Name
newTypeButton.Parent = loadout.TypesList
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"))
if UnlockedStatus then
loadout.BuyDescFrame.BuyButton.Interactable = false
loadout.BuyDescFrame.BuyButton.LockedUI.Visible = true
loadout.BuyDescFrame.EquipButton.Interactable = true
loadout.BuyDescFrame.EquipButton.LockedUI.Visible = false
else
loadout.BuyDescFrame.BuyButton.Interactable = true
loadout.BuyDescFrame.BuyButton.LockedUI.Visible = false
loadout.BuyDescFrame.EquipButton.Interactable = false
loadout.BuyDescFrame.EquipButton.LockedUI.Visible = true
end
loadout.BuyDescFrame.WeaponNameLabel.Text = weaponChild.Name
--add a description and an image preview for the weapon
local damage = StatsModule.BaseDamage
local firerate = StatsModule.FireRate
local reload = StatsModule.ReloadTime
local magcapacity = StatsModule.AmmoPerMag
local spread = StatsModule.Spread
local bulletspeed = StatsModule.BulletSpeed
local walkspeed = SettingModule.WalkSpeed
local basicstats = statgui.Container.StatTypeFrame.BasicStats
local advancedstats = statgui.Container.StatTypeFrame.AdvancedStats
basicstats.DamageLabel.Text = "Damage per bullet: " .. damage --dmg
basicstats.FireRateLabel.Text = "Fire rate: " .. firerate .."s" -- firerate
basicstats.RldSpeedLabel.Text = "Reload time: " .. reload .."s" -- reload
basicstats.MagCapacityLabel.Text = "Mag capacity: " .. magcapacity -- mag
basicstats.SpreadLabel.Text = "Spread: " .. spread .. "°" -- spread
basicstats.BulletSpeedLabel.Text = "Bullet speed: " .. bulletspeed .." studs/s" -- bullet speed
basicstats.WalkSpeedLabel.Text = "Walk speed when equipped: " .. walkspeed .." studs/s" -- walk speed
currentMultiplierTable = StatsModule.DamageMultipliers
--headframe.MouseLeave:Connect(function() humhitFrame.LabelDesc.Text = "Hit multipliers:" end)
--[[if weaponType.Name == "Primary" then
primary = weaponChild
script.Parent.StrPrimary.Value = weaponChild.Name
elseif weaponType.Name == "Secondary" then
secondary = weaponChild
script.Parent.StrSecondary.Value = weaponChild.Name
elseif weaponType.Name == "Melee" then
melee = weaponChild
script.Parent.StrMelee.Value = weaponChild.Name
end]]
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(weaponName)
if weaponName == weaponChild.Name then
loadout.BuyDescFrame.BuyButton.Interactable = false
loadout.BuyDescFrame.BuyButton.LockedUI.Visible = true
loadout.BuyDescFrame.EquipButton.Interactable = true
loadout.BuyDescFrame.EquipButton.LockedUI.Visible = false
loadout.WeaponsList[weaponName].LockedUI.Visible = false
end
end)
--indicating hit multipliers, it should be there because it wont work anywhere else :(
local humhitFrame = statgui.Parent.AdvancedPlus.HumHitMap
local headframe = humhitFrame.HeadFrame
local torsoframe = humhitFrame.TorsoFrame
local rightarmframe = humhitFrame.RightArmFrame
local leftarmframe = humhitFrame.LeftArmFrame
local rightlegframe = humhitFrame.RightLegFrame
local leftlegframe = humhitFrame.LeftLegFrame
local function update(partName)
humhitFrame.LabelDesc.Text = "Hit multiplier: " .. (currentMultiplierTable[partName] or "1")
end
headframe.MouseEnter:Connect(function() update("Head") end)--humhitFrame.LabelDesc.Text = "Hit multiplier: " .. headmulti
torsoframe.MouseEnter:Connect(function()update("Torso") end)
rightarmframe.MouseEnter:Connect(function()update("Right Arm") end)
leftarmframe.MouseEnter:Connect(function()update("Left Arm") end)
rightlegframe.MouseEnter:Connect(function()update("Right Leg") end)
leftlegframe.MouseEnter:Connect(function()update("Left Leg") end)
--==================buy and equip events fire=====================
local buyEvent = rs.WeaponSystemEvents.Buy
local equipEvent = rs.WeaponSystemEvents.Equip
local Price = getpriceEvent:InvokeServer(weaponChild.Name)
print(Price, "price for", weaponChild.Name, UnlockedStatus)
local function ClosePrompt()
ActualUI.BuyPrompt.Visible = false
end
loadout.BuyDescFrame.BuyButton.MouseButton1Click:Connect(function()
ActualUI.BuyPrompt.Visible = true
ActualUI.BuyPrompt.QuestionLabel.Text = "Are you sure you want to buy this " .. weaponChild.Name .. " for:"
ActualUI.BuyPrompt.PriceLabel.Text = Price .. "CC?"
end)
ActualUI.BuyPrompt.Cancel.MouseButton1Click:Connect(function() ClosePrompt() end)
ActualUI.BuyPrompt.Close.MouseButton1Click:Connect(function() ClosePrompt() end)
ActualUI.BuyPrompt.OK.MouseButton1Click:Connect(function() buyEvent:FireServer(weaponChild.Name) end)
loadout.BuyDescFrame.EquipButton.MouseButton1Click:Connect(function()
equipEvent:FireServer(weaponChild.Name)
if weaponType.Name == "Primary" then
updateLoadout(weaponChild.Name, nil, nil)
elseif weaponType.Name == "Secondary" then
updateLoadout(nil, weaponChild.Name, nil)
elseif weaponType.Name == "Melee" then
updateLoadout(nil, nil, weaponChild.Name)
end
end)
end
end)
end
end)
end
rs.WeaponSystemEvents.GetEquipped.OnClientEvent:Connect(function(t)
local primary = nil
local secondary = nil
local melee = nil
for wpntype, wpn in pairs(t) do
print(type, wpn, "was equipped previously")
if wpn then
if wpntype == "Primary" then
primary = wpn
elseif wpntype == "Secondary" then
secondary = wpn
elseif wpntype == "Melee" then
secondary = wpn
end
end
end
task.wait(0.5)
updateLoadout(primary, secondary, melee)
end)