What Is the Best Way to Communicate Between Local Scripts

I am currently working on a combat game, and I’ve come across an issue. I want the player to be able to do only one attack at a time, so I created a variable that is turned off or on whether or not the player is in the middle of an attack. I then checked this before the player could make another attack.

I want to be able to communicate this busy value across multiple scripts. What would be the best way to go about that?

So far I’ve thought of making a boolValue in replicated storage and checking it whenever nessecary, but I want to know if any better options are available.

4 Likes

Not entirely sure what you’re asking for but I think you should learn about Remote Events for this

Hi, You can create a server script and store the combat state of every player in a table and get/modify the value using remote events.

For example, creating a table and on player load and make it false

local combatStates = {}

Players.PlayerAdded:Connect(function(player)
combatStates[player.UserId] = false --false; player not engaged in combat
end)

You can make a module to store the state that can be changed.

3 Likes

Regarding Communication with Local Scripts, a Bindable event works
But I think a Module Script storing the previously mentioned Variable inside the Module would work just fine.

i might be wrong do so idk

There are several ways to communicate between two local or two server scripts. The way I find the most useful is using principles found in Event Driven Architecture.

Ideally, you can set up a subscription to a property and publish an event to that subscription once the state is updated.

Another approach is to use a channel which can trigger an update event once you invoke the channel. This channel can be used by both local scripts.

And finally, we can use async bindable events, this involves using bindable events which can be found in the ROBLOX API.

2 Likes

there are many ways you can do it just do it the way you are comfortable with + good for performance

  • you can use a module script that stores these values - (i prefer this)
  • you can use bindable events/functions
  • or attributes - (this is a good option too)
  • or object values -(i donot prefer this)

also you will need to share the value/make server checks so players willnot exploit and use many attacks at single time

2 Likes

For server → server and client → client communication you can use BindableEvents:

--LocalScript 1
local signal = Instance.new("BindableEvent")
signal.Name = "Signal"
signal.Parent = script

task.wait(5) --wait for 5 seconds, so the other local script loads
signal:Fire("Hello", 5, true) --send some data
--LocalScript 2
local signal = script1:WaitForChild("Signal")

signal.Event:Connect(function(...)
	print(...) --Hello, 5, true
end)
1 Like