More Efficient Scripting (Local to Server script Remote Events) [SOLVED]

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    I have 16 parts that give the player a tool (each tool is different, 16 total). Once the tool is given the tool can be copied onto one of 16 parts AND be given back to the player. Each time it is copied over it is deleted from the player.character so it looks like it is picking it up and putting it back down.

  • What potential improvements have you considered?
    I have considered using tables or arrays, but I do not know how to use them since I have never tried before. I have also posted about my local and server script seperately and gotten help BUT because I never posted about them together until now the help I got did not ultimately work as I wanted.

  • How (specifically) do you want to improve the code?
    I want to improve my already working code by making it shorter and seeing if i can use less remote events. If I were to have a remote event for each tool to be copied onto each part then it would total 256 remote events (16 tool X 16 possible places to copy to) and that is WAY too much.

This is one of the Local Scripts.

local rep = game:GetService("ReplicatedStorage").POVComps.BBComics.CopyComicPlacements
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

function onButton1Down(Player)
	if mouse.Target.Name == "ComicPlacement1" then
		rep.CopyComic1P1:FireServer()
		print("Copying Comic 1 to Placement 1")
	elseif mouse.Target.Name == "ComicPlacement2" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 2")
	elseif mouse.Target.Name == "ComicPlacement3" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 3")
	elseif mouse.Target.Name == "ComicPlacement4" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 4")
	elseif mouse.Target.Name == "ComicPlacement5" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 5")
	elseif mouse.Target.Name == "ComicPlacement6" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 6")
	elseif mouse.Target.Name == "ComicPlacement7" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 7")
	elseif mouse.Target.Name == "ComicPlacement8" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 8")
	elseif mouse.Target.Name == "ComicPlacement9" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 9")
	elseif mouse.Target.Name == "ComicPlacement10" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 10")
	elseif mouse.Target.Name == "ComicPlacement11" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 11")
	elseif mouse.Target.Name == "ComicPlacement12" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 12")
	elseif mouse.Target.Name == "ComicPlacement13" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 13")
	elseif mouse.Target.Name == "ComicPlacement14" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 14")
	elseif mouse.Target.Name == "ComicPlacement15" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 15")
	elseif mouse.Target.Name == "ComicPlacement16" then
		rep.CopyComic1P2:FireServer()
		print("Copying Comic 1 to Placement 16")
	end	
end
mouse.Button1Down:Connect(onButton1Down)

This is one of the Server Scripts. The “Comic1Tool, Comic2Tool, etc” are the tools given to the plyer to copy over and pick onto one of 16 parts.

local repplcmt = game:GetService("ReplicatedStorage").POVComps.BBComics.CopyComicPlacements
local reptools = game:GetService("ReplicatedStorage").POVComps.BBComics.ComicTools
local ComicPlacement = script.Parent

repplcmt.CopyComic1P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic1Tool:Clone()
	Player.Character:FindFirstChild("Comic1Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic2P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic2Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic3P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic3Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic4P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic4Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic5P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic5Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic6P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic6Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic7P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic7Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic8P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic8Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic9P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic9Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic10P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic10Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)
repplcmt.CopyComic11P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic11Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic12P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic12Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic13P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic13Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic14P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic14Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic15P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic15Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

repplcmt.CopyComic16P1.OnServerEvent:Connect(function(Player)
	local ComicTool = reptools.Comic2Tool:Clone()
	Player.Character:FindFirstChild("Comic16Tool"):Destroy()
	ComicTool.Parent = game.Workspace
	ComicTool.Handle.Position = ComicPlacement.Position
	ComicTool.Handle.Orientation = ComicPlacement.Orientation
	ComicTool.Handle.Anchored = true
end)

I’ve seen a lot of codes like this and the answer to almost all of them is to use something like for i, v Codes, because they can show you anything is chosen

1 Like

is for i, v the same as a for loop? Someone had suggested I use that and I did, but it only worked on my first tool copying to my first part, but not the rest. How would I use for i, v for this?

Ok I’m not a programmer but like if the name of your tools is the same you could do like make it check the number of tools you have, check the name of the tool and after the tool there’s the number, like ToolHEREGOESTHENUMBER this way you would be able to get which tool you are getting, and after that run your code, I’m not a programmer so sorry if it was a bad explanation :sweat_smile:

1 Like

lol it’s ok. I’m not one either I am still learning to script! But thank you I will try to look into using for i,v and hopefully go from there! Thanks!

This is what you would be trying to do, basically there’s the name with the number checks if the chosen one has the right number if it does then makes the code go

Yeah that’s who helped me before but the for 1 = 1,16 only worked on my first tool. :confused: Maybe I should retry witt that again.

Have you named ALL of the tools and gave them numbers? Because if you did not then it’s not gonna work

Yeah, so each tool is called “Comic1Tool, Comic3Tool, Comic3Tool” etc. and the same is for the parts I can put them on “ComicPlacement1, ComicPlacement2, ComicPlacement3” etc.

They putting the number at the end, maybe it doesn’t work if the value is in the middle?…

Wait ok so idk what I had did wrong but this DOES work for my local script!

local rep = game:GetService("ReplicatedStorage").POVComps.BBComics.CopyComicPlacements
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()

function onButton1Down(Player)
	for i = 1, 16 do
		if mouse.Target.Name == ("ComicPlacement" .. i) then
			rep["CopyComic1P" .. i]:FireServer()
			print("Copying Comic 1 to Placement" .. i)
			break
		end
	end
end
mouse.Button1Down:Connect(onButton1Down)

However now I want to be able to shorten my server script connected to this because it is still very lengthy.

Ok now on the server script try copying changing the names and variables and paths maybe it’s gonna work and also put the numbers at the end, maybe it’s gonna work

1 Like

Yeah I am guessing I could use for i = 1, 16 for this as well? Like use it on the placement parts the same way it is used on the tools since there are 16 of the parts like the tools.

Yeah it would work like that I think

1 Like

Can’t you just do this? Haven’t tested this yet so I don’t know if it will work

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
local Event = ReplicatedStorage.Events:FindFirstChild(Mouse.Target.Name)
if Event then
Event:FireServer()
print(“Event exist”)
else
print(“Event does not exist”)
end
end)

1 Like

I will try to get back to this in the upcomming days with an update, hopefully in the meantime someone else pitches in and might have a better solution for shortening the server script.

edited Wait no, this would be for the local script and it is working now. Someone else had already helped in a seperate post but I had done something wrong and thought it didn’t work. Now I am trying to also shorten my server script in maybe the same way.

Maybe try this? Again didn’t test this yet

for _, v in pairs(ReplicatedStorage.Events:GetChildren()) do
v.OnServerEvent:Connect(function(Player)
print(“Event fired”)
local ComicTool = ReplicatedStorage.Tools:FindFirstChild(v.Name)
Player.Character:FindFirstChildOfClass(“Tool”):Destroy()
ComicTool.Parent = workspace
ComicTool.Handle.CFrame = ComicPlacement.CFrame
ComicTool.Handle.Anchored = true
end)
end

it gives me an error

Workspace.ComicPlacement1.ComicPlacement1Script:9: attempt to index nil with 'Parent' 

What are your events and tools called?