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.
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
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.
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:
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.
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:
How the Tool looks
Basically for this experiement the relevant part is that FireScript calls the Server script which calls:
SendCastEvt:FireAllClients(…params…)
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.
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.
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
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
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
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.
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().