How to make it so when 2 scripts with the same intention play simultaneously, they don't cancel

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make it so if Script A plays, it doesn’t cancel Script B (They both have the same intention/task of disabling the enemy’s backpack/toolbar), sorry if it’s confusing, it’s kind-of difficult to explain.

  1. What is the issue? Include screenshots / videos if possible!

Script A cancels-out Script B if they both simultaneously play.

Script A Cancelling Script B (The Duration of the Tool Bar being Disabled is Significantly Shorter

Script B Behaving Correctly w/o Cancelling (The Tool Bar stays Disabled for the Correct Amount of Time

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to look if someone had a similar problem, however I couldn’t find anything. I’m sure someone has likely been in the same scenario, I likely just can’t word it correctly.

Code: A

local EnemyScript = script.DisableEnemysToolbar:Clone()
EnemyScript.Parent = enemyHum
EnemyScript.Enabled = true

Code B:

local EnemyScript = script.DisableEnemysToolbar:Clone()   
EnemyScript.Parent = enemyhum
EnemyScript.Enabled = true

Code C: (the script both A, and C, are referring to)


game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
task.wait(3)
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
task.wait()
script:Destroy()

I would recommend placing the function that disables the toolbar inside of a module, and require the module in both Script A and B. This would allow you to add a debounce to the function that temporarily prevents it from activating again if another script already did

1 Like

Thanks for the suggestion, shortly after adding the module, I noticed an error
“StarterGui:SetCoreGuiEnabled must be called from a local script.”. I guess this means I can’t use a module for this task, unfortunately.

That’s impossible, as if you code that function as a module function and run the function on a local script it shall never error like that. May you please elaborate and show me your module script, and how you require&use it?

Well, the function actually is required/played on a server script within a tool. (maybe that’s the issue?)

This is how I’ve prepared it:

Module Script:

local module = {}
local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
task.wait(3)
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
task.wait()

return module

How it’s required/used in the tool script:

local Module = require(game.ReplicatedStorage.Modules.ToolBarDisabler)
Module(enemyhum, 1)

Yes, you need to require it on a localscript

2 Likes

Well, that’s unfortunate, do you think there could be another work-around code I can insert into my scripts that will disable the victim from using their toolbar, other than disabling their CoreGui entirely? Like for example I’m using a bool value to disable them from being able to punch during the animation, but when I used that same method to disable their toolbar the same problem arose.

You could use a remote event, fire it on the server, and when the client receives the remote have it check if the toolbar is already disabled (bool value) and if so then return, and if not then set the toolbar to disabled and mark the disabled bool value as true

1 Like

You can try this, I didn’t test it in Studio so let me know if there’s any errors

(you will have to make a RemoteEvent in ReplicatedStorage called “HideBackpackEvent”)

Serverscript:

local event = game.ReplicatedStorage:WaitForChild("HideBackpackEvent")

event:FireClient(plr)

Localscript:

local event = game.ReplicatedStorage:WaitForChild("HideBackpackEvent")

event.OnClientEvent:Connect(function()
	game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	task.wait(3)
	game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	task.wait()
end)

After about an hour, I finally figured it out. I liked your thought of checking if the toolbar is already disabled or enabled through another bool value and it gave me an idea, so I took your advice, except rather than using a remote event I simply checked if the value was true or not and added the necessary events to ensure the backpack got disabled whenever it was required. Thanks a lot!

1 Like

This, unfortunately, doesn’t work either because after the event starts the “task.wait(3)” in script A continues to play into script B.

The localscript would have replaced script A.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.