I went to look at UIS, and noticed there were no example scripts. For something like UIS, which is one of the largest APIs, this needs some love.
Here’s a script I made that can be an example script.
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer -- grab the local player
local mouse = player:GetMouse() -- this will allow us to check what's being clicked
userInputService.InputEnded:connect(function(input) -- InputEnded first when the input ends, in this case, when the left button is released
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- When the player is using the left mouse button
if mouse.Target ~= nil then -- clicking the sky calls an error
local clickedPart = mouse.Target
if clickedPart.ClassName == "Part" then
clickedPart.BrickColor = BrickColor.Random()
end
end
end
end)
Again, UIS is a huge API, so let’s make some developer examples for new users. Feel free to use any part of the API too!
Your example is pretty good, but for a code example, I would try to make the practices as modern and clean as they could be.
Here’s what it looks like cleaned up a bit.
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer --get the player
local Mouse = Player:GetMouse() --get their mouse
UserInputService.InputEnded:connect(function(Input) --listen for when mouse is clicked
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if Mouse.Target and Mouse.Target:IsA("BasePart") then --check if there is a target, check if it's a part
mouse.Target.BrickColor = BrickColor.random() --change its BrickColor to random
end
end
end)
But yeah, I agree that UserInputService could definitely use some more examples on how to use the various API- since it’s probably the most useful tool for input.
One other thing, we should encourage ContextActionService whenever possible. UIS is certainly important to understand, but most ROBLOX input and UI are using CAS. Since CAS creates input stacks, it is much cleaner when trying to integrate with the built in systems.