Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you stop there being more than one instance of the same remote event for a player at a time?

Asked by 4 years ago
Edited 4 years ago

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:

01local weaponButton = script.Parent
02 
03local ReplicatedStorage = game:GetService("ReplicatedStorage")
04local weaponLevelCheckFunction = ReplicatedStorage:WaitForChild("WeaponLevelCheckFunction")
05local weaponLevelRequested = 0 -- Change me based on the level of the weapon
06 
07weaponButton.MouseButton1Up:Connect(function()
08    print("Loadout Change Request Received")
09    weaponLevelCheckFunction:InvokeServer(weaponLevelRequested) -- Invokes the function and sends the requested level
10end)

Serverside Code:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local WeaeponLevelCheckFunction = ReplicatedStorage:WaitForChild("WeaponLevelCheckFunction")
03 
04WeaeponLevelCheckFunction.OnServerInvoke = function(player, levelRequirement) -- Fires when the player clicks on an item in the loadout GUI
05 
06    player.CharacterAdded:Wait() -- Wait for character to respawn
07    local toolToEquipFolder = game:GetService("ServerStorage"):WaitForChild("SpoonModelStorage"):WaitForChild(levelRequirement) -- Selects which tool to grab based on level requirement
08    local toolToEquip = toolToEquipFolder:FindFirstChildWhichIsA("Tool"):Clone() -- Grabs the tool inside the previously selected folder
09    local playerLevel = player.leaderstats.Level.Value -- Finds the player's level (Serverside)
10 
11    if playerLevel >= levelRequirement then
12 
13        --
14        toolToEquip.Parent = player.Backpack
15        print("Tool Equipped")
16    else
17        print("Player level is not high enough to equip this item")
18    end
19end
0
please include your code Cynical_Innovation 595 — 4y
0
I have updated the post to include the code (Apologies for not doing so initially, I am new to the platform) FlashHawkAssassin 0 — 4y

Answer this question