Mouse Raycast GUI (open source)

I have seen some people releasing their projects so i wanted to try it out let me know if it is useful or if you would use it
what is it exactly? its basically just a script that will show the cost and name of an instance in a billboard gui when you hover over it for example
image

image

  • Is it useful
  • Would you use it?
  • Have any suggestions or fixes for performance

TUTORIAL

  • Here is how i made the gui for anyone who wants to know
    image

  • make a folder named HoverOverGuis if you want to change the name you will have to change the variable path for example = local HoverOverGuis = game.Workspace.DifferentName

image

  • insert a local script into StarterPlayer > StarterPlayerScripts

image

  • copy and paste or choose to read the code
local camera = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local LocalPlayer = game.Players.LocalPlayer


local function mouseRaycast()
	local mousePos = uis:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y)
	
	local nonHoverparts = {}
	
	for _, part in pairs(workspace:GetDescendants()) do
		if not part:HasTag("HoverGui") then
			table.insert(nonHoverparts, part)
		end
	end

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {LocalPlayer.Character}
	params.FilterType = Enum.RaycastFilterType.Exclude

	local result = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 25, params) --change 25 to the max distance
	return result
end

local HoverOverGuis = game.Workspace.HoverOverGuis

game:GetService("RunService").RenderStepped:Connect(function()
	local raycast = mouseRaycast()

	if raycast then
		local mainGui = raycast.Instance:FindFirstChild("MainGui")
		if mainGui then
			local frame = mainGui:FindFirstChild("Frame")
			if frame then
				mainGui.Enabled = true
				mainGui.Frame.Details.InstanceName.Text = raycast.Instance.Name
				mainGui.Frame.Details.Cost.Text = "Cost: "..raycast.Instance:GetAttribute("Cost")
				local TweenServ = game:GetService("TweenService")
				local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
				local tween = TweenServ:Create(frame, tweenInfo, {Size = UDim2.new(1, 0, 1, 0)})
				tween:Play()
			end
		else
			for _, gui in pairs(HoverOverGuis:GetChildren()) do
				gui.MainGui.Enabled = false
			end
		end
	else
		for _, gui in pairs(HoverOverGuis:GetChildren()) do
			local mainGui = gui:FindFirstChild("MainGui")
			mainGui.Enabled = false
		end
	end
end)

Important for non-readers

  • the name of your instances will be what shows as the name on the gui

image

  • The cost is an attribute of the instance so for example the donut has a number attribute named “Cost” with the value as 100 i used a number value but i think you can use other things like strings have NOT tested yet

image

Want To Know How It Works?

  1. It gets the current camera, user input service, and local player from the game environment.

  2. A function mouseRaycast is defined. This function calculates a ray from the camera through the mouse position. It also collects all parts in the workspace that don’t have the “HoverGui” tag.

  3. It sets up parameters for a raycast operation, excluding the local player’s character from the raycast.

  4. The function performs the raycast operation and returns the result.

  5. It connects a function to the RenderStepped event of the RunService, which runs every frame. This function calls mouseRaycast.

  6. If the raycast hits an instance, it checks if the instance has a child named “MainGui”. If it does, it enables the GUI and updates its details.

  7. If the raycast doesn’t hit an instance, it disables all “MainGui” instances in the HoverOverGuis folder.

Example Usage

image

image

image

image

3 Likes

This is more of a #resources:community-tutorials topic than a #resources:community-resources topic

is it? its a resource and tutorial so i think it goes in either or but thats just my opinion.

The post body just looks more like it belongs to #resources:community-tutorials

1 Like

I agree with this, this should instead be restructured to be a tutorial on how to figure out what the mouse is hovering over and display information from a GUI. This use-case being shown here is wayyy to specific and niche.

1 Like

Alex, long time no see. but I still agree that the OP needs to restructure it

1 Like

The code looks really scuffed, Here is why:

  1. The nonHoverParts is an unused variable inside the mouseRaycast() function, so minus that
  2. Instead of typing UDim2.new(1, 0, 1, 0), You can just type UDim2.fromScale(1, 1), It is the same thing
  3. Roblox already provides the workspace variable for you, so you can use that instead of game.Workspace

i dont use fromScale i forgot about nonHoverParts and im used to using game.Workspace it does the same thing so it doesn’t really matter and is just a preference