Making a remote event only be received by 1 script at a time

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 know the title isnt worded too well, Im going to try and word this the best I can.
I have a tool that has two parts, a local script and a serverscript. When the player clicks(Activates) the tool the local script fires an event which reports the players mouse position to the server, then the script under the same tool, recieves the event with the Mouse postion and uses that to throw the tool in the direction that was clicked. Pretty simple.

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

BUT! Ive somehow done it in a such a way so that if two players, are holding the same tool, and 1 of them clicks, the OTHER player’s tool which has the same script, is triggered by the event and THEIR tool then gets thrown to the first players mouse.

You can see the issue. Im not every good with Remote events and Ive looked on the forum and I found a guy with a similar problem but it never got solved, or he never posted that it did.

Is there a way I can setup the event so that when the local script activates the event, it then only gets received by the same player’s tool? Or is there just a whole easier way Im not seeing?

This is the Local Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Tool = script.Parent
local Player = Players.LocalPlayer
print(Player)
local Mouse = Player:GetMouse()

local ThrowEvent = ReplicatedStorage:WaitForChild("ThrowBrickEvent")

Tool.Activated:Connect(function()
	-- Fire the server event with the mouse's target position
	ThrowEvent:FireServer(Mouse.Hit.Position)
end)

And this is the beginning of the function in the serverscript (also found under the same tool)

ThrowEvent.OnServerEvent:Connect(function(player, mouseHitPosition)
    -- the actual function works so I wont include it to save space, but this is how I fire it.
end)

I had a similar issue a few weeks ago! Here’s what I did to solve it:

In the server script, you are taking in the player argument. You can check if the script is a descendant of the character or the player’s backpack to check if it is owned by the player. For example:

ThrowEvent.OnServerEvent:Connect(function(player, mouseHitPosition)
    local character = player.Character
    local backpack = player.Backpack

    if tool:IsDescendantOf(backpack) or tool:IsDescendantOf(character) then
        -- throw the tool here
    end
end)

This should ensure that the tool is actually owned by the player sending the event. The reason we check if the tool is a descendant of the character OR the backpack is because tools get parented to the character when they are equipped, and there is a chance the player unequips the tool, sending it back into the backpack before the event reaches the server.

This is untested, but I hope you understand the general idea!!

1 Like

I just tested it and it works! Thanks a lot! It makes sense idk why I didnt think of it sooner lol.

1 Like

Great! Good luck with your game!

1 Like

Also, a quick side note, have you checked what happens if a player throws their tool and unequips it mid-throw? Make sure that doesn’t cause issues!

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