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

How to use / as a keybyte?

I am making a custom chat, which uses the same thing as kinda like roblox's chat but I disabled the coregui for chat. This works in studio but doesnt work in player so It must be something to do with the keybyte() == 47 anyone know how to enable it?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local able = false
local default = script.Default

function onkey(key)
	if key:byte() == 47
		then
		script.Parent:TweenSizeAndPosition(UDim2.new(1,0,.05,0), UDim2.new(0,0,.45,0), "Out", "Quad", .5, true)
		script.Parent.TextXAlignment = "Center"
		script.Parent:CaptureFocus()
		able = true
	end
end

function clear()
	script.Parent:TweenSizeAndPosition(UDim2.new(1,0,.025,0), UDim2.new(0,0,.975,0), "Out", "Quad", .5, true)
	script.Parent.TextXAlignment = "Left"
	able = false
	script.Parent.Text = default.Value
end

mouse.KeyDown:connect(onkey)
script.Parent.FocusLost:connect(clear)

5 comments

0

Is this in a localscript?

0

mhm

1

I'm pretty sure you can just a string for slash, like 'if key == "/" then end'

0

.... nope

0

You can do ... if key == "/" then ... I've done it before, and it works just fine.

1 answer

✓ Accepted answer

I'd recommend using UserInputService instead of KeyDown as it will take in any human interaction (including keyboard presses) regardless of game state.

Also you don't have to mess around with key bytes (some keys have different byte codes in studio vs client, some have the same as others, etc) and can just use the KeyCode Enum instead.

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Slash then
		--code
	end
end

1 comment

0

Thanks :)

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