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

Anyway I Can Shorten this piece of code?

Maybe I can shorten any piece of this code, All of this code makes a little function to the game... Maybe I'm adding something to extra?

local clickJoinGreenRoom = workspace.JoinGame.RedGameJoin.Part.ClickDetector

local RedRoomSpawnPoints = {
    CFrame.new(229.5, 2, 232),
    CFrame.new(234.5, 2, 232),
    CFrame.new(238.5, 2, 232),
    CFrame.new(238.5, 7, 232),
    CFrame.new(238.5, 11, 232),
    CFrame.new(233.5, 11, 232),
    CFrame.new(229.5, 11, 232),
    CFrame.new(224.5, 11, 232),
    CFrame.new(214.5, 6, 232),
    CFrame.new(217.5, 3, 232),
    CFrame.new(224.5, 7, 232),
    CFrame.new(229.5, 14, 232),
    CFrame.new(222.5, 14, 232),
    CFrame.new(238.5, 10, 232),
}
		local redPlayerOnePoints = 0
		local redPlayerTwoPoints = 0 

		local signPlayerOne = workspace.RedRoom.SignPlayerOne.SurfaceGui.Text
		local signPlayerTwo = workspace.RedRoom.SignPlayerTwo.SurfaceGui.Text


local playersInRedRoom = {}
local redWinPart = workspace.RedRoom.Win

-- PLAYER CLICKS THE PART TO JOIN GAME AND IF HE IS ALREADY ON THE GAME HE DOESNT JOIN
clickJoinGreenRoom.MouseClick:Connect(function(player) -- 
	player.PlayerGui.ScreenGui.Text1.Visible = true
	player.PlayerGui.ScreenGui.Text2.Visible = true
	if #playersInRedRoom < 2 then  
		table.insert(playersInRedRoom, player.Name) -- INSERTS PLAYERS ON THE TABLE
		
		if player.Name == playersInRedRoom[1] then
			signPlayerOne.Text = game:GetService("Players")[playersInRedRoom[1]].Name.." Has Points: "..redPlayerOnePoints
			game.Players[playersInRedRoom[1]].PlayerGui.ScreenGui.Text1.Text =  game:GetService("Players")[playersInRedRoom[1]].Name.." Has Points: "..redPlayerOnePoints
			game:GetService("Players")[playersInRedRoom[1]].Character:SetPrimaryPartCFrame(CFrame.new(223, 3.5, 240.5)) -- TELEPORT THE FIRST PLAYER
			game:GetService("Players")[playersInRedRoom[1]].Character.HumanoidRootPart.Anchored = true
			print("Player one is "..game:GetService("Players")[playersInRedRoom[1]].Name)
		end -- PLAYER NAME
		
		-- IF PLAYER 2 CLICKED THE WIN PART
		if player.Name == playersInRedRoom[2] then
			signPlayerTwo.Text = game:GetService("Players")[playersInRedRoom[2]].Name.." Has Points: "..redPlayerTwoPoints
			game.Players[playersInRedRoom[2]].PlayerGui.ScreenGui.Text2.Text =  game:GetService("Players")[playersInRedRoom[2]].Name.." Has Points: "..redPlayerTwoPoints
			game:GetService("Players")[playersInRedRoom[2]].Character:SetPrimaryPartCFrame(CFrame.new(223, 3.5, 223.5)) -- TELEPORT THE SECOND PLAYER
			game:GetService("Players")[playersInRedRoom[2]].Character.HumanoidRootPart.Anchored = true
			print("Player two is "..game:GetService("Players")[playersInRedRoom[2]].Name)
		end
		
		if #playersInRedRoom == 2 then    -- CURRENT
			print("Starting in 3...")
			wait(1)
			print("Starting in 2...")
			wait(1)
			print("Starting in 1...")
			wait(1)
				
			redWinPart.CFrame = RedRoomSpawnPoints[math.random(1, #RedRoomSpawnPoints)] -- RANDOM TELEPORT THE WIN PART
			
							
			redWinPart.ClickDetector.MouseClick:Connect(function(click) -- CLICKING THE WIN PART THEN TELEPORTS		
					
			-- IF PLAYER 1 TWO CLICKS THE WIPART THEN
			if click.Name == playersInRedRoom[1] then
				redPlayerOnePoints = redPlayerOnePoints + 1
				print("Player 1 got a point")
				game.Players[playersInRedRoom[1]].PlayerGui.ScreenGui.Text1.Text =  game:GetService("Players")[playersInRedRoom[1]].Name.." Player One Points: "..redPlayerOnePoints
				game.Players[playersInRedRoom[2]].PlayerGui.ScreenGui.Text1.Text =  game:GetService("Players")[playersInRedRoom[1]].Name.." Player One Points: "..redPlayerOnePoints
				signPlayerOne.Text = game:GetService("Players")[playersInRedRoom[1]].Name.." Player One Points: "..redPlayerOnePoints
				redWinPart.CFrame = RedRoomSpawnPoints[math.random(1, #RedRoomSpawnPoints)]
			end
		
			-- IF PLAYER 2 TWO CLICKS THE WIPART
			if click.Name == playersInRedRoom[2] then
				redPlayerTwoPoints = redPlayerTwoPoints + 1
				print("Player 2 got a point")
				game.Players[playersInRedRoom[1]].PlayerGui.ScreenGui.Text2.Text =  game:GetService("Players")[playersInRedRoom[2]].Name.." Player Two Points: "..redPlayerTwoPoints
				game.Players[playersInRedRoom[2]].PlayerGui.ScreenGui.Text2.Text =  game:GetService("Players")[playersInRedRoom[2]].Name.." Player Two Points: "..redPlayerTwoPoints
				signPlayerTwo.Text = game:GetService("Players")[playersInRedRoom[2]].Name.." Player Two Points: "..redPlayerTwoPoints
				redWinPart.CFrame = RedRoomSpawnPoints[math.random(1, #RedRoomSpawnPoints)]
		
		
		
		
			-- IF PLAYER ONE WON THE GAME THEN
			if redPlayerOnePoints == 10 then
			game:GetService("Players")[playersInRedRoom[1]].Character:SetPrimaryPartCFrame(CFrame.new(202, 95, 254)) -- TELEPORT THE FIRST PLAYER
					print(playersInRedRoom[1].." Won the game!")
					redWinPart.CFrame = CFrame.new(224, 27.5, 226)
					table.remove(playersInRedRoom, #playersInRedRoom)
				end	
				
			-- IF PLAYER TWO WON THE GAME THEN	
			if redPlayerTwoPoints == 10 then
			game:GetService("Players")[playersInRedRoom[2]].Character:SetPrimaryPartCFrame(CFrame.new(202, 95, 254)) -- TELEPORT THE FIRST PLAYER
					print(playersInRedRoom[2].." Won the game!")
					redWinPart.CFrame = CFrame.new(224, 27.5, 226)
					table.remove(playersInRedRoom, #playersInRedRoom)	
				end
			end	
			end)	
		end    
 
	end--         
end)-- END Mouseclick                       





1 answer

Variables.

If a value is being used more than about 3 times (or when you feel like it) then you would need a variable.

Sample (when it's being overused):

Before:

local lunchable = {'ham';'ham';'ham';'ham';'ham';'ham';'cheese';'cheese';'cheese';'cheese';'cheese';'cheese';'cracker';'cracker';'cracker';'cracker';'cracker';'cracker';}; 
-- too long

After:

local a = 'ham';
local b = 'cheese';
local c = 'cracker';

local lunchable = {a;a;a;a;a;a;b;b;b;b;b;b;c;c;c;c;c;};
-- not the best but better

Based on your script you tend you index players, or use the getservice function to get service players. That clearly needs a variable.

5 comments

0

But How am I supposed to get the index of players on a variable, I tried: local RedPlayersOne = game:GetService("Players")[playersInRedRoom[1]] but it doesn't work

0

i edited it

0

supposed to be service players, so you can put local Players = game:GetService'Players'; and replace alot of stuff that use the service Players

0

As I said, i tried it and it didn't work

0

Show me how you do it

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