Example Scripts For UIS In Wiki

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!

@UristMcSparks (tagged since he owns the wiki group)

Edit: Fixed formatting

1 Like

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.

The Studio team is responsible for the wiki, you should move it to Studio Features. (see pinned post there about wiki requests)

First off, discourse hates me. What code block is that? I use [code] but I can’t get breaks.

Second, I personally do, just all the wiki examples I see don’t wrap them like that.

@buildthomas
Alright, thanks.

You can use four spaces or tab for each line of code, for breaks, you just need to put a tab (or four spaces) on a new line.

But… my OCD. :frowning: Srsly…

Edit: It seems I need to use the ``` tag.

If you use ``` code ``` indenting should work fine.

1 Like

I’ll start writing some examples.
Thanks for letting me know.

So long as they follow this: http://wiki.roblox.com/index.php?title=Roblox_Wiki:Style_Guide extra code examples (especially on the API pages) are welome.

1 Like

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.

3 Likes

My code style is pretty much that, except I use camelCase for all variable names.