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

How do I do damage numbers and OnKey functions?

This is me reposting my other question in more detail!

Probably a big topic, but I was browsing around the internet for scripting lessons on making advanced swords and stuff, and all I saw was scripting for beginners and things like that. For example, in the game 'Black Magic', they have weapons with showing damage numbers, and hotkey actions. I'm looking for a tutorial on how to do those two things, but so far I've found nothing. Here are some example of scripts I think I can use for this situation, this first script-box shows creating a damage gui:

function billboard(pos,text,time,color)
	local pos=pos or Vector3.new(0,0,0)
	local text=text or "Hello World!"
	local time=time or 2
	local color=color or Color3.new(1,1,0)
	local pos=pos+Vector3.new(0,5,0)
	local ep=Instance.new("Part")
	ep.Name="Effect"
	ep.formFactor="Custom"
	ep.Size=Vector3.new(0,0,0)
	ep.TopSurface="Smooth"
	ep.BottomSurface="Smooth"
	ep.CFrame=CFrame.new(pos)
	ep.Anchored=true
	ep.CanCollide=false
	ep.Transparency=1
	local bb=Instance.new("BillboardGui")
	bb.Size=UDim2.new(3,0,3,0)
	bb.Adornee=ep
	local tl=Instance.new("TextLabel")
	tl.BackgroundTransparency=1
	tl.Size=UDim2.new(1,0,1,0)
	tl.Text=text
	tl.TextColor3=color
	tl.TextScaled=true
	tl.Font="ArialBold"
	tl.TextStrokeTransparency = 0

	tl.Parent=bb
	bb.Parent=ep
	debris:AddItem(ep,time+.1)
	ep.Parent=game.Workspace
	delay(0,function()
		local frames=time/rate
		for frame=1,frames do
			wait(rate)
			local percent=frame/frames
			ep.CFrame=CFrame.new(pos)+Vector3.new(0,5*percent,0)
			tl.TextTransparency=percent
		end
		ep:remove()
	end)
end

Im still not sure how to make OnKey functions yet, that would also be helpful! Other than just having regular attacks, mabey an action on pressing Q or E or something like that might work, here's what I think:

local Key = value.Key
		local Down = value.Down
		if Key == "q" and Down then

Is that the right one? Well, thank you for reading, if you have an answer then put it in the reply box below!

1 answer

✓ Accepted answer

There are different ways to detect keyboard inputs, but the preferred method is through UserInputService.InputBegan:

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.UserInputType.Name=="Keyboard"then
		local key=input.KeyCode.Name
		if key=="q"then
			-- ...
		end
	end
end)

The old method is (if you are using a Tool) using Mouse.KeyDown which automatically ignores keypresses when the user is typing in a textbox/chatting, but it is a deprecated feature.

To detect textbox focus (chat) so you can ignore key commands when somebody is really just chatting, you can use TextBoxFocused and TextBoxFocusReleased:

local uis=game:GetService("UserInputService")

local chatting=false
uis.TextBoxFocused:connect(function()chatting=true end)
uis.TextBoxFocusReleased:connect(function()chatting=false end)

uis.InputBegan:connect(function(input)
	if input.UserInputType.Name=="Keyboard"then
		if chatting then return end
		local key=input.KeyCode.Name
		if key=="q"then
			-- ...
		end
	end
end)

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