Finding the square root when the player messages a number

Code:

game.Players.PlayerAdded:Connect(function (player)
player.Chatted:Connect(function(msg)
		if msg == "find the square root of " -- this is where it checks what the number is, but I dont know what to do.
		
	end)
end)

Then, I want to connect it to a variable like this:

game.Players.PlayerAdded:Connect(function (player)
player.Chatted:Connect(function(msg)
		if msg == "find the square root of " then
		local number = --the number the player says
	end)
end)
1 Like
local num = tonumber(string.match(msg, "%d+"))
local root = math.sqrt(num)

like this?

game.Players.PlayerAdded:Connect(function (player)
	player.Chatted:Connect(function(msg)
		if msg == "find the square root of " then
			local num = tonumber(string.match(msg, "%d+"))
			local root = math.sqrt(num)
			print(root)
			end
		end)
end)

this doesn’t work

Try this :

game.Players.PlayerAdded:Connect(function (player)
	player.Chatted:Connect(function(msg)
		if string.match(msg:lower(), "find the square root of ") then
			local num = tonumber(string.match(msg, "%d+"))
			local root = math.sqrt(num)
			print(root)
			end
		end)
end)
1 Like

Not like that.

player.Chatted:Connect(function(msg)
    if string.match(string.lower(msg), "find the square root of ") then
        -- my code here
    end
end)

@Minhseu123 and @anon_j1124 thanks for the help. But I have another problem, I dont know how to do equations like 2 + 1 or 3 x 2,

elseif string.match(msg:lower(), "find the sum of (number) and (number)") then

I want it to find the sum of two numbers then add the numbers and print it out.

This should help.

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