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

Trying to think of a way to make a gui "activate" at X distance?

Asked by
lomo0987 250 Moderation Voter
9 years ago

What I am currently trying to do is make a script that will only show up at X distance.. I know I am going to need to use Magnitude to see how far a person is from a spot. I have done some testing and I was able to create a local script within a player to see if the player was within the distance of a brick that was specified.. But specifying each brick for each gui that I would want to show would be a tedious thing. I don't want anyone to create the script for me. I would rather do it myself but I just need a few tips on how I would go about creating it. Like the idea behind it.

What i have done before was this..

wait(2)
local Player = game.Players.LocalPlayer
local Char = Player.Character.Torso
local mouse = Player:GetMouse()
local Box = game.Workspace.AmmoBox
local ammo = Box.Ammo
local maxammo = Player.MaxAmmo
local pammo = Player.Ammo
local mag = (Box.Position - Char.Position).Magnitude
local Storage = Player.Storage
local MStorage = Player.MaxStorage

function keydown(key)
    key = key:lower()
    print(key)
    if key == "f" then
        if Box then
            if mag >= 10 then
                if Storage.Value < MStorage.Value then
                    Storage.Value = Storage.Value + math.floor(MStorage.Value*.7)
                end
            end
        end
    end
end

mouse.KeyDown:connect(keydown)

how this script works is that it's inside the StarterGUI as a local script. It defines the player and the box. Defines the Magnitude of the player to the box. If the player presses the "f" key, it checks if the player is within 10 studs of the box, if so then it goes through with awarding the player ammo.

I have been thinking of ways on how to implement what I have done before to what I want to currently do and it's a bit of an odd situation for me.

What I have been thinking is something like this..

owner = script.Parent.Parent.Owner -- This defines the owner of the tycoon
player = game.Workspace:WaitForFirstChild(owner.Value) -- This currently defines the character. 
Char =  player.Torso
brick = script.Parent
mag =  (brick.Position - Char.Position).Magnitude --gets the Magnitude

-- I don't know any way to currently effectively check automatically if the player is within a certain magnitude I am going to need some help with that.
-- If I was to try to do something it would be "mag.changed:connect" but I have no idea if that even works considering it's just a variable within the script
--What would happen after that is the GUI that is currently named "Terminal" would become visible/invisible depending on the mag. game.Players.PlayerGui.Terminal.Frame

All I would simply need is a nudge in the correct direction :D So don't take the fun out of scripting and do it for me XD The most I would ask for is a link to the wiki.

1
Instead of using Magnitude, use :GetDistanceFromCharacter(brick.Position) EzraNehemiah_TF2 3552 — 9y
0
uhh, why is one over the other any better? lomo0987 250 — 9y
0
Less to type @lomo. Also, you could just have a spherical part covering the distance that you want the gui to be inserted. Insert with Touched event. Remove with TouchEnded event. Goulstem 8144 — 9y
0
Hmm, that seems to be a decent way to do it. lomo0987 250 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Since you want to check this regularly, you need either a loop while true do wait() --[[check here]] end or an event player.Character.Torso.Changed(function(prop) if prop == "Position" then --[[check here]] end end).

Since you want to compare the player's location against a number of objects, use a table/list:

objects = {
    workspace.Ammo,
    workspace.SomethingElse.SpecialBrick,
    --etc
}
--OR, if everything's in the same model:
objects = workspace.SpecialBricks:GetChildren()
--OR if everything's in the workspace and has the same name:
objects = {}
for _,v in workspace:GetChildren() do
    if v.Name == "PutNameHere" then
        table.insert(objects, v)
    end
end
--OR maybe all the bricks have a Gui in them, with a special name, meaning we can find them using a recursive function:
objects = {}
function FindObjects(parent)
    if parent.Name == "PutNameHere" and parent.ClassName == "ScreenGui" then
        table.insert(objects, parent.Parent) --insert the brick containing the gui
        return
    end
    for _,v in parent:GetChildren()
        FindObjects(v)
    end
end
FindObjects(workspace)

Once you have your table of objects, you just need to iterate over them (again, either in the while loop or in the event's function). ex:

while true do
    wait()
    for i = 1, #objects do
        --check to see if player is close to objects[i] and do something about it. Consider storing the "closestObject" and using that after this loop completes (useful if you have multiple objects that are very close to each other).
    end
end

Alternative possibility: Use SurfaceGuis. They can be placed in the StarterGui and, so long as their Adornee property is valid, will not require additional scripting (just a script to receive input from it).

Ad
Log in to vote
-1
Answered by 9 years ago

maybe put a block x amount of distance away and have the gui show when you step on the block link it to the gui that is where you want it to show

Answer this question