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

Making a gui get displayed only at a certain distance?

I've managed to make a script that makes a text label visible when it hovers over a model. My question is, how do I; A) Make a table so it works on multiple models B) Make the gui be displayed only at a distance no larger than 16.

Thanks in advance.

Here's the code:

local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local ActiveParts = workspace:WaitForChild("Door1")

local Tag = script.Parent
Tag.Text = "CLICK TO OPEN"


RunService.RenderStepped:connect(function()
    local Target = Mouse.Target -- Get the object the mouse is hovering over
    if Target and ActiveParts:IsAncestorOf(Target) then -- Check if the part is anywhere inside the model

        
        Tag.Size = UDim2.new(0.1,Tag.TextBounds.X,0,25) 
        Tag.Visible = true

    -- If no target exists, then...
    else

        -- Hide the tag
        Tag.Visible = false

    end 

    -- Update the tag's position with the mouse
    Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)

2 answers

###How To Fix

A) After you make the table, you iterate through it using a generic for loop. Compare the mouse's target with values in the table.

B) Compare the magnitude of the difference in positions between the mouse's target and your character.

#####Efficiency Fixes

  • You should use the Mouse.Move event, rather than firing every frame :)
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local ActiveParts = {"Door1","Door2"} --These are what activate the ui
local Tag = script.Parent
Tag.Text = "CLICK TO OPEN"

repeat wait() until plr.Character

local char = Player.Character
local root = char:WaitForChild("HumanoidRootPart")
local minimunDist = 16 --Minumun distance to activate ui

Mouse.Move:Connect(function()
	local Target = Mouse.Target
	local dist = (Target.CFrame.p - root.CFrame.p).magnitude --Get distance
	if dist <= minimumDist then --Check distance
		for _,v in next,ActiveParts do --Iterate through table
			if v == Target.Name then --Compare values
				Tag.Position = UDim2.new(
					0,
					Mouse.X-Tag.AbsoluteSize.X,
					0,
					Mouse.Y-Tag.AbsoluteSize.Y
				)
				Tag.Size = UDim2.new(0.1,Tag.TextBounds.X,0,25) 
				Tag.Visible = true
			else
				Tag.Visible = false
			end
		end
	end
end)

3 comments

0

I guess i posted a bit late. Don't bother my answer under this one if this works

0

Players.Player1.PlayerGui.ScreenGui.Frame.TextLabel.re:8: attempt to index global 'plr' (a nil value)

0

Sorry I always use the variable 'plr' when defining the client :)

This script works but it only has one problem, the range won't chance as you move. If you can fix that you will be good to go.

This is how you should place stuff:

StarterGui - Imgur

Workspace - Imgur

This is the VariableHandler:

--[[
	This script won't work if players decide to be R15 or not
--]]

local player = game:GetService("Players").LocalPlayer
local char = player.Character
local torso = char.Torso  -- change char.Torso to char.UpperTorso if the game is R15
local ActiveParts = workspace:WaitForChild("Door1")
local WALL = ActiveParts.wall
_G.WALLrange = (torso.Position - WALL.Position).magnitude

and this is the TextHandler:

local RunService = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local char = player.Character
local torso = char.Torso
local hum = char.Humanoid
local mouse = player:GetMouse()

local ActiveParts = workspace:WaitForChild("Door1")

local interface_magnitude = 6.5

local Tag = script.Parent
Tag.Text = "CLICK TO OPEN"

Tag.Visible = false

RunService.RenderStepped:connect(function()
	local tpos = torso.Position
    local Target = mouse.Target -- Get the object the mouse is hovering over	
	 if Target and ActiveParts:IsAncestorOf(Target) then
		if _G.WALLrange >= interface_magnitude then
			
			Tag.Size = UDim2.new(0.1,Tag.TextBounds.X,0,25) 
        Tag.Visible = true

        -- If no target exists, then...
        else

        -- Hide the tag
        Tag.Visible = false	
			
		end
		end
	-- Update the tag's position with the mouse
        Tag.Position = UDim2.new(0,mouse.X-Tag.AbsoluteSize.X,0,mouse.Y-Tag.AbsoluteSize.Y)
        end)

1 comment

0

Players.Player1.PlayerGui.ScreenGui.TextLabel.TextHandler:22: attempt to compare number with nil

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