Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Anyway to make this code more efficient and smaller?

Im confused what to add for the Koala1,2 and 3 as I plan to have around 50 of them.

local enabled = true
local ding = game.Workspace.Sounds.Ding				-- sound
local part = game.Workspace.Configuration.Koala1	-- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local gui = player:WaitForChild("PlayerGui")
local toolup = gui:WaitForChild("clickE")
local maxpickup = 20
local KoalaGUI = player.PlayerGui.KoalaAdd


	local pp = player.leaderstats.Credits
	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	
		local item = mouse.Target
		local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB then
		ding:Play()
		local newkoala = KoalaGUI:Clone()
		newkoala.Parent = game.Players.LocalPlayer.PlayerGui
		newkoala.Enabled = true
		part.Position = Vector3.new(100,100,100)
		enabled = false
		
		part.Transparency = 1
		part.CanCollide = false
	
		game.ReplicatedStorage.KoalaEvent:FireServer()
		
wait(5)
		newkoala:Destroy()
		enabled = true
		part.Transparency = 0
		part.CanCollide = true
		
	
end
	end)
local enabled = true
local ding = game.Workspace.Sounds.Ding				-- sound
local part = game.Workspace.Configuration.Koala2	-- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local gui = player:WaitForChild("PlayerGui")
local toolup = gui:WaitForChild("clickE")
local maxpickup = 20


	local pp = player.leaderstats.Credits
	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	
		local item = mouse.Target
		local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB then
		ding:Play()
		local newkoala = KoalaGUI:Clone()
		newkoala.Parent = game.Players.LocalPlayer.PlayerGui
		newkoala.Enabled = true
		part.Position = Vector3.new(100,100,100)
		enabled = false
		
		part.Transparency = 1
		part.CanCollide = false
	
		game.ReplicatedStorage.KoalaEvent:FireServer()
		
wait(5)
		newkoala:Destroy()
		enabled = true
		part.Transparency = 0
		part.CanCollide = true
		
	
end
	end)
local enabled = true
local ding = game.Workspace.Sounds.Ding				-- sound
local part = game.Workspace.Configuration.Koala3	-- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local gui = player:WaitForChild("PlayerGui")
local toolup = gui:WaitForChild("clickE")
local maxpickup = 20


	local pp = player.leaderstats.Credits
	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	
		local item = mouse.Target
		local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB then
		ding:Play()
		local newkoala = KoalaGUI:Clone()
		newkoala.Parent = game.Players.LocalPlayer.PlayerGui
		newkoala.Enabled = true
		part.Position = Vector3.new(100,100,100)
		enabled = false
		
		part.Transparency = 1
		part.CanCollide = false
	
		game.ReplicatedStorage.KoalaEvent:FireServer()
		
wait(5)
		newkoala:Destroy()
		enabled = true
		part.Transparency = 0
		part.CanCollide = true
		
	
end
	end)

1 comment

0

There is a wait(5) you had in your code, you can remove it to reduce the wait time to zero

1 answer

✓ Accepted answer

Improvements

Use :WaitForChild() to make sure an instance exists before using it

Use :GetService() for the Workspace, Players, and ReplicatedStorage services

You shouldn't have to redefine local variables as they remain the same, the only one you would have to adjust is "part"

Explanation

If you use a table, you can create all the connections you want at once without reiterating code. In the below, the condition is that the instance is a child of "Configurations" and the first 5 characters in the name are "Koala"

Revised Server Script

local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local mouse = player:GetMouse()
local pp = player:WaitForChild("leaderstats"):WaitForChild("Credits")

local Sound = Workspace:WaitForChild("Sounds"):WaitForChild("Ding")
local KoalaEvent = game:GetService("ReplicatedStorage"):WaitForChild("KoalaEvent")

local GuiFolder = player:WaitForChild("PlayerGui")
local ToolUp = GuiFolder:WaitForChild("clickE")
local KoalaGui = GuiFolder:WaitForChild("KoalaAdd")

local maxpickup = 20
local enabled = true

for _, Part in pairs(Workspace:WaitForChild("Configuration"):GetChildren()) do
	if string.sub(Part.Name, 1, 5) == "Koala" then
		UserInputService.InputBegan:Connect(function(input, event)
			local item = mouse.Target
			local distance = player:DistanceFromCharacter(item.Position)
			if mouse.Target == Part and enabled == true and distance <= maxpickup and (input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonB) then
				Sound:Play()
		
				local newkoala = KoalaGui:Clone()
				newkoala.Parent = GuiFolder
				newkoala.Enabled = true
		
				enabled = false
		
				Part.Position = Vector3.new(100, 100, 100)
				Part.Transparency = 1
				Part.CanCollide = false
		
				KoalaEvent:FireServer()
				wait(5)
				newkoala:Destroy()
				enabled = true
				Part.Transparency = 0
				Part.CanCollide = true
			end
		end)
	end
end

1 comment

0

Thanks, for some reason it doesnt let me pick up another koala for 5 seconds though

This discussion is preserved as it was. New answers, comments, and votes are closed.