Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Making a gui get displayed only at a certain distance?

Asked by 8 years ago

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:

01local RunService = game:GetService("RunService")
02local Player = game:GetService("Players").LocalPlayer
03local Mouse = Player:GetMouse()
04 
05local ActiveParts = workspace:WaitForChild("Door1")
06 
07local Tag = script.Parent
08Tag.Text = "CLICK TO OPEN"
09 
10 
11RunService.RenderStepped:connect(function()
12    local Target = Mouse.Target -- Get the object the mouse is hovering over
13    if Target and ActiveParts:IsAncestorOf(Target) then -- Check if the part is anywhere inside the model
14 
15 
View all 29 lines...

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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 :)
01local RunService = game:GetService("RunService")
02local Player = game.Players.LocalPlayer
03local Mouse = Player:GetMouse()
04local ActiveParts = {"Door1","Door2"} --These are what activate the ui
05local Tag = script.Parent
06Tag.Text = "CLICK TO OPEN"
07 
08repeat wait() until plr.Character
09 
10local char = Player.Character
11local root = char:WaitForChild("HumanoidRootPart")
12local minimunDist = 16 --Minumun distance to activate ui
13 
14Mouse.Move:Connect(function()
15    local Target = Mouse.Target
View all 33 lines...
0
I guess i posted a bit late. Don't bother my answer under this one if this works araltan2002 47 — 8y
0
Players.Player1.PlayerGui.ScreenGui.Frame.TextLabel.re:8: attempt to index global 'plr' (a nil value) Aperturee 22 — 8y
0
Sorry I always use the variable 'plr' when defining the client :) Goulstem 8144 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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:

01--[[
02    This script won't work if players decide to be R15 or not
03--]]
04 
05local player = game:GetService("Players").LocalPlayer
06local char = player.Character
07local torso = char.Torso  -- change char.Torso to char.UpperTorso if the game is R15
08local ActiveParts = workspace:WaitForChild("Door1")
09local WALL = ActiveParts.wall
10_G.WALLrange = (torso.Position - WALL.Position).magnitude

and this is the TextHandler:

01local RunService = game:GetService("RunService")
02 
03local player = game:GetService("Players").LocalPlayer
04local char = player.Character
05local torso = char.Torso
06local hum = char.Humanoid
07local mouse = player:GetMouse()
08 
09local ActiveParts = workspace:WaitForChild("Door1")
10 
11local interface_magnitude = 6.5
12 
13local Tag = script.Parent
14Tag.Text = "CLICK TO OPEN"
15 
View all 37 lines...
0
Players.Player1.PlayerGui.ScreenGui.TextLabel.TextHandler:22: attempt to compare number with nil Aperturee 22 — 8y

Answer this question