!speed command not working properly

You can write your topic however you want, but you need to answer these questions:

Hi, I am trying to achieve a !speed [number] script! The range i want is 16-32, but when I would type a number lower then 16 or higher than 32, it would give me the error message i want but still change the speed to the number they put!

Video:
robloxapp-20221010-1409476.wmv (1.2 MB)

Here’s the code I have so far!

local modules = require(game.ServerScriptService.CustomCommands.Modules.CommandsModule)
local sendmessage = game.ReplicatedStorage.SendCommandMessage

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local check = modules.checkGamePass(plr.UserId,"Manager")
		if check == "Owned" then
			local words = string.split(msg, " ")
			local command = words[1]
			local amount = words[2]
			if command == "!speed" then
				local speed = 0
				speed = plr.Character.Humanoid.WalkSpeed
				if amount == "16" or amount == "17" or amount == "18" or amount == "19" or amount == "20" or amount == "21" or amount == "22" or amount == "23" or amount == "24" or amount == "25" or amount == "26" or amount == "27" or amount == "28" or amount == "29" or amount == "30" or amount == "31" or amount == "32" then
					plr.Character.Humanoid.WalkSpeed = amount
					sendmessage:FireClient(plr, "[Command]: You have successfully set your walk speed to "..amount.."! Chat '!resspeed' to reset your speed back to the normal speed!", Color3.new(0, 0.854902, 0))
				elseif amount ~= "16" or amount ~= "17" or amount ~= "18" or amount ~= "19" or amount ~= "20" or amount ~= "21" or amount ~= "22" or amount ~= "23" or amount ~= "24" or amount ~= "25" or amount ~= "26" or amount ~= "27" or amount ~= "28" or amount ~= "29" or amount ~= "30" or amount ~= "31" or amount ~= "32" then
					sendmessage:FireClient(plr, "[Command]: You cannot set your Walk Speed to "..amount.."! You can only set it between 16 and 40!", Color3.new(1, 0, 0))
				end
		elseif check == "NotOwned" then
			elseif msg == "!speed" then
			sendmessage:FireClient(plr, "[Command]: You do not have permissions to use this command! You need to own the 'Manager' game pass!", Color3.new(1, 0, 0))
		end
		end
	end)
end)

Everything on the script works fine, except for the number range. There are no errors in the output.

Any help would be appreciated!

Instead of typing out this long if-then condition, try:

if tonumber(amount) >= 16 and tonumber(amount) <= 32 then

Same goes for the elseif line. Assuming “amount” is a string, you could convert that to number using tonumber() function to convert it to number and compare it if it matches the condition. More info on string conversion, check: Strings | Roblox Creator Documentation

In addition, instead of using or for the range, use inequality symbols >, <, <=, >=.
For example, say x >= 16, means if x is greater than equal to 16 then […] So saying:

if x >= 16 and x<= 32 then

Is like saying, if x is less than equal to 32 but more than equal to 16. (aka range from 16-32)
More info check: Intro to If Statements | Roblox Creator Documentation

Does your code give any errors? In line 15 it should give an error because
plr.Character.Humanoid.WalkSpeed is a number and amount is a string.

Personally I would not suggest to make a really complicated system like admin commands without knowing basics of scripting like the >, <, operators as @QUlCKRED said

2 Likes

Thank you for the suggestion! I will try it now!

I tried this solution but it did not work! Thank you for the suggestion for how I can fix it!

I know the basics of it, but it is a string so it would not work on that.

Did it print out any errors in output?

^^ Also did u also convert this “amount” to number as well? ^^
To:

plr.Character.Humanoid.WalkSpeed = tonumber(amount)

Just note, the variable “amount” is still a string. To make it simpler, you should try to change the variable “amount” to local amount = tonumber(words[2]), so that it is already a number.
Hence, for the if statements you do:

if amount >= 16 and amount <= 32 then
plr.Character.Humanoid.WalkSpeed = amount

Thank you! I will try that now!

It did not work. I really do appreciate your help! Sorry if it’s an easy solution and I seem dumb, I just am not really seeing why it still sets the player’s speed to the amount they put in when the error chat message appears!

Other commenters have already provided a solution, but maybe you just need the typed-out version. Make sure to mention any errors.

local newSpeed = tonumber(words[2])

if newSpeed and newSpeed >= 16 and newSpeed <= 32 then
  plr.Character.Humanoid.WalkSpeed = newSpeed
  -- Send message
else
  -- Speed is either lower than 16, higher than 32 or isn't a number
end
1 Like

Thank you for assisting! I appreciate it!