There are 3 areas that you’ll have to focus on here.
- StarterGui
- StarterPlayerScripts (located in StarterPlayer)
- ReplicatedStorage
Here is a step by step process to get you to your goal.
-
StarterGui
Put a ScreenGui inside of the StarterGui
-
ReplicatedStorage
Put a TextLabel inside of ReplicatedStorage
-
StarterPlayerScripts
Put a local script inside of StarterPlayerScripts. Next you’ll want to get access to some things you’ll need in the future. In this case, you’ll need ContextActionService (to detect mouse input), ReplicatedStorage (to find your TextLabel), your TextLabel (for creating the numbers), and finally your PlayerGui (to put the TextLabels in). Here is what your script should look like:
local ContextActionService = game:GetService('ContextActionService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ScreenGui = game.Players.LocalPlayer:WaitForChild('PlayerGui', 20):WaitForChild('ScreenGui', 20)
local TextLabel = ReplicatedStorage:WaitForChild('TextLabel', 20)
Now that you have access to what you need, you can code the input function when the player clicks using ContextActionService:BindAction() This function of ContextActionService needs a function to call during input, so we’ll also create a function called OnClick Inside that function, we’ll check if the input is Begin or End to see if the mouse is up or down. If it’s End, we will stop because we only want the function to run once per mouse click. Your code should now look like this.
local ContextActionService = game:GetService('ContextActionService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ScreenGui = game.Players.LocalPlayer:WaitForChild('PlayerGui', 20):WaitForChild('ScreenGui', 20)
local TextLabel = ReplicatedStorage:WaitForChild('TextLabel', 20)
local function OnClick(Name, InputState, InputObject)
--Check that the mouse was just clicked
if not (InputState == Enum.UserInputState.Begin) then
return
end
end
ContextActionService:BindAction(
'Click',
OnClick,
false,
Enum.UserInputType.MouseButton1
)
Now we can add the rest of what happens during a mouse click into that function OnClick. To create a text label, we’ll call our variable TextLabel and use TextLabel:Clone() to create a new one. We’ll parent it to the screen GUI, give it a random position, and change the text to a random number. Finally, we’ll use task.wait(3) to wait 3 seconds before destroying the text label.
Here is what your final code should look like:
local ContextActionService = game:GetService('ContextActionService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local ScreenGui = game.Players.LocalPlayer:WaitForChild('PlayerGui', 20):WaitForChild('ScreenGui', 20)
local TextLabel = ReplicatedStorage:WaitForChild('TextLabel', 20)
local function OnClick(Name, InputState, InputObject)
--Check that the mouse was just clicked
if not (InputState == Enum.UserInputState.End) then
return
end
--Get your random position (using the AbsoluteSize property of your ScreenGui)
local RandomPosition = UDim2.new(0, math.random(0, ScreenGui.AbsoluteSize.X), 0, math.random(0, ScreenGui.AbsoluteSize.Y))
local Label = TextLabel:Clone()
Label.Parent = ScreenGui
Label.Position = RandomPosition
--Create your random number
Label.Text = '+'..math.random(1, 100)
task.wait(3)
Label:Destroy()
end
ContextActionService:BindAction(
'Click',
OnClick,
false,
Enum.UserInputType.MouseButton1
)
If any step in this process is too difficult to understand, I recommend watching youtube videos on how to achieve something like this. They will be able to show you clearly and visually.