How can I improve my "repetitive" script?

Hello everyone, I kinda am new to scripting and i’ve realized that my code looks bad and I’ve used a lot of elseifs, I will take any advice to improve this and what can I replace In my code (like anything)?

The script Is for a phone that gets pressed whenever the player says something, the script Is In ServerScriptService, thank you.

local Players = game:GetService("Players")
local numberSound = game.Workspace.Phone.Call.Halflifebutton --gotta play everytime the button is pressed
local canPress = true

local phonePad = { --buttons on the phone
	number0 = game.Workspace.Phone.Zero,
	number1 = game.Workspace.Phone.one,
	number2 = game.Workspace.Phone.two,
	number3 = game.Workspace.Phone.three,
	number4 = game.Workspace.Phone.four,
	number5 = game.Workspace.Phone.five,
	number6 = game.Workspace.Phone.six,
	number7 = game.Workspace.Phone.seven,
	number8 = game.Workspace.Phone.eight,
	number9 = game.Workspace.Phone.nine,
	shutIt = game.Workspace.Phone.Shut,
	CallIt = game.Workspace.Phone.Call,
}

local function moveX(partToMove)
	if canPress == true then
		canPress = false
		
		numberSound:Play()
		partToMove.Position = partToMove.Position + Vector3.new(0.1, 0, 0)
		partToMove.Color = Color3.new(0, 0.666667, 0)
		
		wait(0.6)
		
		canPress = true
		
		partToMove.Position = partToMove.Position - Vector3.new(0.1, 0, 0)
		partToMove.Color = Color3.new(0.654902, 0.654902, 0.654902)
	else
		return
	end
end

Players.PlayerAdded:Connect(function(plr) --fires everytime a player joins
	plr.Chatted:Connect(function(message) --fires everytime a player chats
		if message == "0" then --check for the message number
			moveX(phonePad.number0)
		elseif message == "1" then
			moveX(phonePad.number1)
			elseif message == "2" then
			moveX(phonePad.number2)
			elseif message == "3" then
				moveX(phonePad.number3)
			elseif message == "4"  then
				moveX(phonePad.number4)
		elseif message == "5"  then
			moveX(phonePad.number5)
		elseif message == "6"  then
			moveX(phonePad.number6)
		elseif message == "7"  then
			moveX(phonePad.number7)
		elseif message == "8"  then
			moveX(phonePad.number8)
		elseif message == "9"  then
			moveX(phonePad.number9)
		elseif message == "call" then
			moveX(phonePad.CallIt)
		elseif message == "kill" then
			moveX(phonePad.shutIt)
		else
			print("test123")
		end
	end)
end)

It looks like you can change the keys from that phonePad object to be the strings you’re looking for. Then instead of having the lines checking the chat string and converting that into the value in phonePad, you can just take it directly:


local phonePad = { --buttons on the phone
	["0"] = game.Workspace.Phone.Zero,
	["1"] = game.Workspace.Phone.one,
	["2"] = game.Workspace.Phone.two,
	["3"] = game.Workspace.Phone.three,
	["4"]  = game.Workspace.Phone.four,
	["5"] = game.Workspace.Phone.five,
	["6"] = game.Workspace.Phone.six,
	["7"] = game.Workspace.Phone.seven,
	["8"] = game.Workspace.Phone.eight,
	["9"] = game.Workspace.Phone.nine,
	kill = game.Workspace.Phone.Shut,
	call = game.Workspace.Phone.Call,
}


---

if phonePad[message] ~= nil then
moveX(phonePad[message])
end
2 Likes

Even if it’s got a mix between numbers and strings? Wasn’t sure what the behaviour would be. That’s good to know, thanks!

It could lead to script problems and for loops (thats why i deleted the post)

1 Like

nice answer :), another way is to name your objects 0,1,2 etc and kill=10,call=11 then you could


moveX(game.Workspace.Phone:FindFirstChild(tostring(message))

not tested but u get the idea

2 Likes

Thanks yall for the answers, It will probably will take me a while to analyze and understand the code but I’ll try and modify my original one lol

1 Like

Wow man, that really did give me a whole new perspective, thank you for the help

Also can i ask, what purpose does the “nil” at the if statements part serve? Can you explain that as It kinda confuses me

nil means something like non-existent so they check if the part does exist with ~= (basically means not), you can also just type the instance which is practically the same

if part then
-- (only an example)
end
1 Like

So the nil just saves me from having to check for which message was sent everytime?

No the if statement checks if something is not nil and will prevent errors from occuring if for some reason one of the instances don’t exists anymore

(if statement need to be true to run)

1 Like

Okay I believe I got It thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.