How to make a remote event that can be used by multiple similar tools

to be fair i have no clue how to use remote events and the forum isnt very helpful to me

i have a slapping system i found on the toolbox a year back (its basically a slap battles fangameive made for me and my friends) and ive been using and improving it however each tool has this code repeated but with just increased stats depending on the tool

or maybe instead of remote events i could use modules but then again i have 0 knowledge on that aswell

i will give a sample of the first tool you aquire in the game
its made up of 2 scripts
server and local
heres the server

local tool=script.Parent
local handle=tool:WaitForChild("Handle")
tool:WaitForChild("Event").OnServerEvent:Connect(function(plr,mode,target,velocity)
	if mode=="slash" then
		local FightMode = target:WaitForChild("HumanoidRootPart"):WaitForChild("FightMode")
		if FightMode.Parent == target:WaitForChild("HumanoidRootPart") then
			if not target:FindFirstChild("ReverseAbility") and not target:FindFirstChild("TauntG") then
			local char = tool.Parent
			local slaps  =  game:GetService("Players"):GetPlayerFromCharacter(char):FindFirstChild("leaderstats"):FindFirstChild("Slaps")
			
			local MS = game:GetService("MarketplaceService")
			local gamePassID = 172576245
			
			if MS:UserOwnsGamePassAsync(plr.UserId, gamePassID) then
				slaps.Value = slaps.Value + 2
			else
				slaps.Value = slaps.Value + 1
			end
			
			local flingvelocity=velocity*5
			local spinvelocity=velocity*0.5
			local multipliervelocity=tool:WaitForChild("Power").Value*3
			target:FindFirstChildOfClass("Humanoid").PlatformStand=true
			local bv=Instance.new("BodyVelocity")
			bv.Parent=target:WaitForChild("HumanoidRootPart")
			bv.MaxForce=Vector3.new(1e8,1e8,1e8)
			bv.Velocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
			local bav=Instance.new("BodyAngularVelocity")
			bav.Parent=target:WaitForChild("HumanoidRootPart")
			bav.AngularVelocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
			script.Parent.Handle.MeshPart.hit.Enabled = true
			wait(0.1)
			script.Parent.Handle.MeshPart.hit.Enabled = false
			local sound=Instance.new("Sound")
			sound.Parent=target:WaitForChild("HumanoidRootPart")
			sound.Volume=3
			sound.SoundId="rbxassetid://7195270254"
			sound:Play()
			wait(tool:WaitForChild("FlightSpeed").Value)
			bv:Destroy()
			bav:Destroy()
			target:FindFirstChildOfClass("Humanoid").PlatformStand=false
			sound:Destroy()
			elseif target:FindFirstChild("TauntG") then
				local offset = Vector3.new(0,0,5) 
				target.HumanoidRootPart.CFrame = tool.Parent.HumanoidRootPart.CFrame*CFrame.new(offset)
				target.HumanoidRootPart.Anchored = true
				tool.Parent.HumanoidRootPart.Anchored = true
				local folder = game.ReplicatedStorage.TauntGlove
				local no = folder.NO:Clone()
				no.Parent = target.Head
				no:Play()
				wait(0.65)
				target.HumanoidRootPart.Anchored = false
				tool.Parent.HumanoidRootPart.Anchored = false
				target:FindFirstChildOfClass("Humanoid"):LoadAnimation(folder:WaitForChild("Slap")):Play()
				local char = tool.Parent
				local slaps  =  game:GetService("Players"):GetPlayerFromCharacter(target):FindFirstChild("leaderstats"):FindFirstChild("Slaps")

				local MS = game:GetService("MarketplaceService")
				local gamePassID = 172576245

				if MS:UserOwnsGamePassAsync(target.UserId, gamePassID) then
					slaps.Value = slaps.Value + 2
				else
					slaps.Value = slaps.Value + 1
				end
				local player = tool.Parent
				local flingvelocity=velocity*5
				local spinvelocity=velocity*0.5
				local multipliervelocity=tool:WaitForChild("Power").Value*3
				player:FindFirstChildOfClass("Humanoid").PlatformStand=true
				local bv=Instance.new("BodyVelocity")
				bv.Parent=player:WaitForChild("HumanoidRootPart")
				bv.MaxForce=Vector3.new(1e8,1e8,1e8)
				bv.Velocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
				local bav=Instance.new("BodyAngularVelocity")
				bav.Parent=player:WaitForChild("HumanoidRootPart")
				bav.AngularVelocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
				script.Parent.Handle.MeshPart.hit.Enabled = true
				wait(0.1)
				script.Parent.Handle.MeshPart.hit.Enabled = false
				local sound=Instance.new("Sound")
				sound.Parent=player:WaitForChild("HumanoidRootPart")
				sound.Volume=3
				sound.SoundId="rbxassetid://7195270254"
				sound:Play()
				wait(tool:WaitForChild("FlightSpeed").Value)
				bv:Destroy()
				bav:Destroy()
				player:FindFirstChildOfClass("Humanoid").PlatformStand=false
				sound:Destroy()
				no:Destroy()
			elseif target:FindFirstChild("ReverseAbility") then
				local char = tool.Parent
				local slaps  =  game:GetService("Players"):GetPlayerFromCharacter(char):FindFirstChild("leaderstats"):FindFirstChild("Slaps")

				local MS = game:GetService("MarketplaceService")
				local gamePassID = 172576245

				if MS:UserOwnsGamePassAsync(plr.UserId, gamePassID) then
					slaps.Value = slaps.Value + 2
				else
					slaps.Value = slaps.Value + 1
				end

				local flingvelocity=velocity*5
				local spinvelocity=velocity*0.5
				local multipliervelocity=tool:WaitForChild("Power").Value*3
				script.Parent.Parent:FindFirstChildOfClass("Humanoid").PlatformStand=true
				local bv=Instance.new("BodyVelocity")
				bv.Parent=script.Parent.Parent:WaitForChild("HumanoidRootPart")
				bv.MaxForce=Vector3.new(1e8,1e8,1e8)
				bv.Velocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
				local bav=Instance.new("BodyAngularVelocity")
				bav.Parent=script.Parent.Parent:WaitForChild("HumanoidRootPart")
				bav.AngularVelocity=flingvelocity+Vector3.new(0,multipliervelocity,0)
				script.Parent.Handle.MeshPart.hit.Enabled = true
				wait(0.1)
				script.Parent.Handle.MeshPart.hit.Enabled = false
				local sound=Instance.new("Sound")
				sound.Parent=script.Parent.Parent:WaitForChild("HumanoidRootPart")
				sound.Volume=3
				sound.SoundId="rbxassetid://7195270254"
				sound:Play()
				wait(tool:WaitForChild("FlightSpeed").Value)
				bv:Destroy()
				bav:Destroy()
				script.Parent.Parent:FindFirstChildOfClass("Humanoid").PlatformStand=false
				sound:Destroy()
			end
		end
	elseif mode=="glove" then
		script.Parent.Handle.funny_fart1:Play()
	end
end)

and the local

local tool=script.Parent
local handle=tool:WaitForChild("Handle")
local mouse=game.Players.LocalPlayer:GetMouse()
local canslap=false
local cd=false
local abilitycd=false
local abilitycooldown=4
local equipped=false
local abilityactivategui=tool:WaitForChild("AbilityActivateButton")
abilityactivategui:WaitForChild("Button").MouseButton1Down:Connect(function()
	if equipped==true then
		if abilitycd==false then
			abilitycd=true
			tool:WaitForChild("Event"):FireServer("glove")
			abilityactivategui.Button.Text = "Cooldown"
			
			wait(abilitycooldown)
			abilityactivategui.Button.Text = "E"
			abilitycd=false
		end
	end
end)
tool.Equipped:Connect(function()
	equipped=true
	abilityactivategui.Parent=game.Players.LocalPlayer:WaitForChild("PlayerGui")
end)
tool.Unequipped:Connect(function()
	equipped=false
	abilityactivategui.Parent=tool
end)
tool.Activated:Connect(function()
	if cd==false then
		cd=true
		local multiplier=2.25
		local char=game.Players.LocalPlayer.Character
		char:FindFirstChildOfClass("Humanoid"):LoadAnimation(tool:WaitForChild("Swing")):Play()
		canslap=true
		wait(tool:WaitForChild("Speed").Value/multiplier)
		canslap=false
		wait(tool:WaitForChild("Speed").Value-tool:WaitForChild("Speed").Value/multiplier)
		cd=false
	end
end)
mouse.KeyDown:Connect(function(key)
	if key=="e" then
		if equipped==true then
			if abilitycd==false then
				abilitycd=true
				print("ability fired")
				tool:WaitForChild("Event"):FireServer("glove")
				wait(abilitycooldown)
				abilitycd=false
				print("ability")
			end
		end
	end
end)
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.ButtonL2 then
		if equipped==true then
			if abilitycd==false then
				abilitycd=true
				tool:WaitForChild("Event"):FireServer("glove")
				wait(abilitycooldown)
				abilitycd=false
			end
		end
	end
end)
handle.Touched:Connect(function(part)
	if canslap==true then
		if part.Parent:FindFirstChildOfClass("Humanoid") then
			canslap=false
			local char=game.Players.LocalPlayer.Character
			local velocity=char:WaitForChild("HumanoidRootPart").CFrame.LookVector*tool:WaitForChild("Power").Value
			tool:WaitForChild("Event"):FireServer("slash",part.Parent,velocity)
		end
	end
end)

inside the tool is also a remote event called Event :smiley:
and the ability gui
i just want to optimize this as much as possible but i physically do not know where to start

1 Like

You can give each player their own event in their character. Then instead of firing the event inside the tool, you can just fire it inside the player. This way you only have an event for each player.

can you elaborate what you mean?

im not the best coder out there

So right now every part has their own event inside the tool right? Which I believe is what your trying to reduce, the remote events. So instead of having an event for each tool, have an event for each player. All you have to do is add a function and connect it to when a player joins the game(game.PlayerAdded), then find their character, and make an event in it. This way we can just use this event when we have a tool.

also each tool is practically the same, a script, a local script and a remote event
i think i see what you mean tho
how can i reduce the scripts also?

I guess you can just combine both scripts together, this may also allow you to use less events too. You can just replace where they fire the event with whatever script is being ran during the event firing. Just keep in mind that you might still need to keep some script as some are for rendering the bullet on the server. I guess you can also have a main bullet renderer on the server, so everytime someone wants to render a bullet on the server you can just fire the event there.

I’m not completely sure what you mean by similiar but. What you can do is, give all the “similiar tools” like a similiar name. Maybe like for example “sword” at the end. Then you can use the string.match function to check if that word is in the string.

well to be quite frak they are all just gloves on a stick with the exception of a couple tools that only for example admins can use

1 Like

Maybe you can use collectionservice then I guess.

can you explain what collectionservice is?

It’s basically just a service used to make multiple things do the same thing, so like an example would be killparts. Let’s say in your game you have 10 killparts and they all do the same thing. Instead of adding a script into each part, you can add a tag to each one using the tag editor or just adding it in properties and then do this.

for i, part in collectionservice:GetTagged("YourTagNameHere") do
-- Your code in here
end

hm, but with all the tools that are in the game doing different things that could mean a very long script

i mean techincally they do the same thing but its just their passives and abilities

Yeah so then you could use string.match as an alternative, if they all have like a similiar name for example.

ill look into it and try to find a solution