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

Why am i getting a number instead of anyhing else?

Basically, I have this script which is in a textbox that when pressed enter it would shutdown the server with an optional reason, but for some weird reason I'm getting a number instead of words and I have tried to found out the reason but to no avail i could not find the reason. Here is my script:

function Run(args)
	script.Parent.Parent.exe.SE:FireServer("",args)
end

game:GetService("UserInputService").InputBegan:Connect(function(e,gpe)
	if e.UserInputType == Enum.UserInputType.Keyboard then
		if e.KeyCode == Enum.KeyCode.Return then
			local text = script.Parent.Text
			local txt = script.Parent
			if string.find(text:sub(1, 8), "shutdown") then
				local reason = "Game Shutdown! Reason: No Reason Given."
				if string.find(text:sub(10, #text), ".+") then
					print(string.find(text:sub(10, #text), ".+")) -- Prints numbers instead of a string or anything else
					reason = "Game Shutdown! Reason: " .. string.find(text:sub(10, #text), ".+")
				end
				for _,p in pairs(game:GetService("Players"):GetChildren()) do
					Run("game:GetService('Players').".. p.Name .. ":Kick('" .. reason .. "')")
				end
				txt.Text = ''
			end
		end
	end
end)

I have used the same method of getting the words before for other command but just for this command I'm getting this weird error, can someone explain to me what is going on?

Here is what the print outputs when I type lol btw: 1 3

2 comments

0

What's stopping you from doing text:sub(10, #text) instead of string.find(text:sub(10, #text), ".+")

0

oh... uh i completely missed that ill give it a try.

2 answers

✓ Accepted answer

Or, why not just use string.match or str:match instead?

function Run(args)
    script.Parent.Parent.exe.SE:FireServer("",args)
end

game:GetService("UserInputService").InputBegan:Connect(function(e,gpe)
    if e.UserInputType == Enum.UserInputType.Keyboard then
        if e.KeyCode == Enum.KeyCode.Return then
            local text = script.Parent.Text
            local txt = script.Parent
            if string.find(text:sub(1, 8), "shutdown") then
                local reason = "Game Shutdown! Reason: No Reason Given."
                if string.find(text:sub(10, #text), ".+") then
		    -- you maybe shouldn't need the print now
                    reason = "Game Shutdown! Reason: " .. string.match(text:sub(10, #text), ".+")
                end
                for _,p in pairs(game:GetService("Players"):GetChildren()) do
                    Run("game:GetService('Players').".. p.Name .. ":Kick('" .. reason .. "')")
                end
                txt.Text = ''
            end
        end
    end
end)

In this way, it automatically returns the string matched, and you don't need to use string.sub and string.find. It automatically does the string.find function, + it does the string.sub function and takes the text matched automatically! Hope this helps!

1 comment

0

oh wow didnt know that thanks alot it works!

string.find returns the sub or place in number of the string, not the actual string. There's a simple fix to your problem and that's using string.sub! It's a handy tool but shouldn't be over used.

Fixed code:

function Run(args)
    script.Parent.Parent.exe.SE:FireServer("",args)
end

game:GetService("UserInputService").InputBegan:Connect(function(e,gpe)
    if e.UserInputType == Enum.UserInputType.Keyboard then
        if e.KeyCode == Enum.KeyCode.Return then
            local text = script.Parent.Text
            local txt = script.Parent
            if string.find(text:sub(1, 8), "shutdown") then
                local reason = "Game Shutdown! Reason: No Reason Given."
                if string.find(text:sub(10, #text), ".+") then
                    print(string.find(text:sub(10, #text), ".+")) -- Prints numbers instead of a string or anything else
                    reason = "Game Shutdown! Reason: " .. string.sub(string.find(text:sub(10, #text), ".+"))
                end
                for _,p in pairs(game:GetService("Players"):GetChildren()) do
                    Run("game:GetService('Players').".. p.Name .. ":Kick('" .. reason .. "')")
                end
                txt.Text = ''
            end
        end
    end
end)

Comment issues

Accept this answer if it worked

2 comments

0

your one does not show a reason only black, but krowten one works, sorry about that.

0

sorry i mean blank

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