Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why aren't these keys getting added to the string? [SOLVED]

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

This has been solved. The solution was to use the KeyUp event instead of KeyDown.

Original Post:

What I want this to do is once you press /, every time you press a key after that it will be added to a string variable called theString, until you press Enter and it resets. This works, but it will not record certain keys like numbers, i, and o. Is there any way to get around this?

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local addToString = false
local theString = ""

mouse.KeyDown:connect(function(key)
    if key == "/" and not addToString then
        addToString = true
        print("add to String is true") 
    elseif theString.byte(key) == 13 and addToString then --13 = "Enter"
        print(theString) 
        theString = ""
        addToString = false
    elseif theString.byte(key) ~= 13 and addToString then
        theString = theString..key
        print(key.." was added to the String")
    end
end)

1 answer

Log in to vote
1
Answered by
Ekkoh 635 Moderation Voter
9 years ago

You were trying to call a function that doesn't exist. When manipulating strings, you use the string library or you use the methods like so.

theString.byte(key)

Should be

string.byte(key) -- using string library
-- or
key:byte() -- built in method
0
I see that now... But it worked even with theString.byte(key) which doesn't make sense. Perci1 4988 — 9y
0
It worked because usually when you call `byte` as a method, you use a colon. You used a period and you passed it a parameter, so it worked normally. Ekkoh 635 — 9y
0
Weird. Perci1 4988 — 9y
Ad

Answer this question