How to access Events stored in Tool at all times?

Hi

I have a script in StarerPlayerScripts that needs to connect to a BindableEvent and RemoteEvent stored in a tool. Doing character:FindFirstChild("ToolName") does not work because the tool isn’t always equipped, and game:GetService("StarterPack"):FindFirstChild("ToolName") just didn’t work without any errors.

Ideas?

I’m trying to implement FastCast, in particular @UIScript’s suggestion for how to do it.

Already got it working with all the events in ReplicatedStorage, but I’d like to keep as much as possible of the tool’s functionality within the tool for the sake of organisation.

Other suggestions for how I can structure my Events are welcome

2 Likes

Just put the local script in the tool. If it’s working with a tool it should be in a tool.

The LocalScript that needs access to the Tool’s Events is responsible for drawing all the bullets the players see at all times, so it needs to run before the tool (a gun) is equipped and thus can’t be in the tool.

If it doesn’t find the tool in the player’s character then simply check players backpack.
ex:

if not Character:FindFirstChild("ToolName") then
   Tool = Backpack:FindFirstChild("ToolName")

Also you should check whenever the Tool changes parent.
1 Like

Weird, it’s not working for me. But I also didn’t check if the tool changes parent yet. Don’t have to just yet as I just want to see Player2 seeing Player1’s bullet, even before Player2 selects the tool.

How I now set the tool in the LocalScript in StarterPlayerScripts:

Assume I’ve waited on player and character

if not character:FindFirstChild("DebugGun") then
  Tool = Player:FindFirstChild("Backpack"):FindFirstChild("DebugGun")
  print("Tool set to backpack: "..Tool.Name)
else
  Tool = character:FindFirstChild("DebugGun")
  print("Tool set to character: "..Tool.Name)
end

Then I do:
local EventStorage = Tool
local SendCastEvt = EventStorage:WaitForChild("SendCast")

As for receiving the RemoteEvent in that script I have:

SendCastEvt.OnClientEvent:Connect(DrawProjectileAllOtherClients)

function DrawProjectileAllOtherClients(Origin, DirectionWithMagnitude, Speed, cosmeticBulletObject, ignoreDescendantsInstance, ignoreWater, bulletAcceleration, Type)
	print("CastReplicationClient: Received message")
   --More irrelevant code
end

When I have the events in ReplicatedStorage this print is printed on Player2 after Player1 shoots, when I have them in the tool it is not.

What is the variable for the Player? Also, what do you mean by “Player2 seeing Player1’s bullet, even before Player2 selects the tool.” I am really confused by what you mean.

What is the variable for the Player?

local Player = game:GetService("Players").LocalPlayer

local character = Player.Character
if not character or not character.Parent then
    character = Player.CharacterAdded:wait()
end

Player2 seeing Player1’s bullet, even before Player2 selects the tool
How it looks when Events are inside the tool. When events are in ReplicatedStorage, Player2 on the left sees Player1’s bullets:
Image from Gyazo

How the Tool looks
image
Basically for this experiement the relevant part is that FireScript calls the Server script which calls:
SendCastEvt:FireAllClients(…params…)

image
Which is listened for in CastReplicationClient with the code in my post above.

So the Server script tells all the CastReplicationClient scripts now located in all the Players to draw the projectile specified in the params.

If I could just get print line in function DrawProjectileAllOtherClients() (mentioned in prev. reply) to print on Player2, everything will work.

So the issue is that the player has to have the tool equipped to see the bullets?

Issue is the same if tool is equipped :confused:

Currently the issue is that the RemoteEvent “SendCast” seen in the pics does not get noticed by CastReplicationClient if “SendCast” is stored in the tool. But if it’s stored in ReplicatedStorage it works.

Edit: Just did a test, only moving SendCast to ReplicatedStorage while keeping the rest of the events in the tool, and it worked again… So definitely that is the issue.

So the Tool gets noticed and the SendCast returns infinite yield?

Tool is definitely seen by CastReplicationClient because of this line printing the correct tool name (seen earlier):
print("Tool set to backpack: "..Tool.Name)

I don’t think RemoteEvents return anything? So SendCast wouldn’t return infinite yield? (idk what that is tbh)

I’ll try setting up a RemoteFunction and see if it returns

By infinite yield, I meant if the WaitForChild found the SendCast. I am really confused about why it doesn’t get noticed.

CastReplicationClient found SendCast just fine. Tried printing it’s name now and it showed up in both Player1 and Player2’s consoles.

Changing parent from Tool to ReplicatedStrage should make no difference. I am pretty sure the mistake is inside your server script. Would you send the .rbxm file so I can try fixing it myself for you?

I removed most of the redundant stuff from the tool to make it easier for you.

How to re-create:
Start a local server with 2 players in Studio
Select the gun asap (max 8 seconds after server starts) on one of the players
The other player will get the error below

This error only happens if the other player’s CastReplicationClient receives the message from the gun-holding player’s server Script.

Now swap the two lines in Server and CastReplicationClient (I’ve marked them for you) and try again.
Now if you re-do the experiment the other player will not receive the message and thus not error :frowning:

Btw, all the logic except for this one RemoteEvent is removed, so the gun won’t work in your version. It’s just for testing the RemoteEvent

notworking.rbxl (144.2 KB)

1 Like

What exactly do you mean by this?

To show the problem (Player2’s CastReplicationClient does not receive the RemoteEvent SendCast sent by the Server script in Player1) I’ve put the RemoteEvent SendCast both in the Tool and in ReplicatedStorage.

So in my example (notworking.rbxl) you can see that if you change which of the two SendCasts the scripts use, the script CastReplicationClient behaves differenly (either receives the event and runs some code that fails, or does not receive the event and thus not run the code that fails).

And you are saying it doesn’t matter if the RemoteEvent SendCast is in Tool or ReplicatedStorage, but my example shows it really seems to matter. So if you’d like to take a look for yourself I’d appreciate it

I think the problem was that the server script in each player’s Tool couldn’t see the RemoteEvent being fired in another tool. But if it’s in ReplicatedStorage, all the LocalScripts listen to the same RemotEvent and all get triggered when one of the Server scripts uses FireAllClients() on it.

So I have to keep SendCast in ReplicatedStorage

Sorry for the late response, but alternatively you could make script search all characters and backpacks and fire every remote with FireClient() individually instead of FireAllClients().