So bassicaly i have a script in a tool that upon a key press will fire a event from client to server,but the problem,is that every other tool in the game that has that “on server event” script ,will fire.Any solutions?
The simplest way to achieve this would be to have a separate event for each tool by parenting the script that handles it to the tool itself.
i have a lot of tools though,and many of these tools would be in other players inventory also.
If they’re in many other players’ inventories too then so would the script that’s attached to them. You would want to ensure that the RemoteEvent and the LocalScript are also in each tool, so they don’t intefere with the others. Then all you would have to do is define these, specific to the tool, for example:
LocalScript:
local tool = script.Parent
local yourEvent = tool:WaitForChild("YourEvent")
-- KeyPressed:
yourEvent:FireServer()
Its better to just check if the tool in the character of the player has the name your looking for when the remote event gets fired
But there would be at least 50+ tools, and for my game some tools are unlockable,wouldnt that be time consuming?
You can pass the name of the tool in your remote event’s :FireServer() parameters and check it on the server. Though you may want to check the players character instead for security reasons
That’s also an option, just gave that method from personal preference and what I’ve done in the past.
Ah, I was under the assumption that you were just talking about how many times the tool replicated. In that case I’m assuming you’ve got multiple OnServerEvents, which can be made into one just by including arguments and only running actions on the tool you want.
Local:
local RS = game:GetService("ReplicatedStorage")
local yourEvent = RS:WaitForChild("YourEvent")
local tool = script.Parent -- assuming your LocalScript is the parent of the tool
-- KeyPressed:
yourEvent:FireServer(tool) -- may include other arguments if needed
Server:
local RS = game:GetService("ReplicatedStorage")
local yourEvent = RS:WaitForChild("YourEvent")
local toolOne = nil -- define your tools if needed
yourEvent.OnServerEvent:Connect(function(plr, tool)
local plrChar = plr.Character
local findTool = plrChar:FindFirstChild(tool.Name)
if not findTool then return end
-- now you can run specific actions depending on the tool
if findTool == toolOne then
print("Player wants to do an action with this specific tool")
end
end)
If you want to keep multiple server events then I would just return the event if it’s not equal to the tool that you want for that event.