I'm having an issue, and I can't seem to find any information on how to fix it online. I'm trying to make a loadout script, where when a player clicks on a GUI element, they are given the corresponding weapon upon respawning. My issue is, if they click a whole bunch of different buttons, it calls a whole bunch of separate remote events which all run the same serverside script, meaning that when they respawn they will have many weapons instead of only one. Is there a way to put something at the beginning of the serverside script that says; "If there are other instances of this script running (for this player), stop those instances, and then run the script?"
Clientside Code:
local weaponButton = script.Parent local ReplicatedStorage = game:GetService("ReplicatedStorage") local weaponLevelCheckFunction = ReplicatedStorage:WaitForChild("WeaponLevelCheckFunction") local weaponLevelRequested = 0 -- Change me based on the level of the weapon weaponButton.MouseButton1Up:Connect(function() print("Loadout Change Request Received") weaponLevelCheckFunction:InvokeServer(weaponLevelRequested) -- Invokes the function and sends the requested level end)
Serverside Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local WeaeponLevelCheckFunction = ReplicatedStorage:WaitForChild("WeaponLevelCheckFunction") WeaeponLevelCheckFunction.OnServerInvoke = function(player, levelRequirement) -- Fires when the player clicks on an item in the loadout GUI player.CharacterAdded:Wait() -- Wait for character to respawn local toolToEquipFolder = game:GetService("ServerStorage"):WaitForChild("SpoonModelStorage"):WaitForChild(levelRequirement) -- Selects which tool to grab based on level requirement local toolToEquip = toolToEquipFolder:FindFirstChildWhichIsA("Tool"):Clone() -- Grabs the tool inside the previously selected folder local playerLevel = player.leaderstats.Level.Value -- Finds the player's level (Serverside) if playerLevel >= levelRequirement then -- toolToEquip.Parent = player.Backpack print("Tool Equipped") else print("Player level is not high enough to equip this item") end end