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

Text Gui and Keystrokes Within Distance of Block?

Asked by 8 years ago

Hi all! I usually have a base line idea of where to start with my problems, but this one I have no idea.

Here's what I want to accomplish: When the Player is within X distance of a block... A text gui will pop up on their screen and The script will respond if a certain key is pushed, in this case spacebar

Any help is greatly appreciated, I have very little experience with magnitude and keystrokes.

1 answer

Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
8 years ago

Well, first we need to get the distance between the player and the block.

--in a localScript
player = game.Players.LocalPlayer
if not player.Character then player.CharacterAdded:wait() end
char = player.Character
block = game.Workspace:WaitForChild("Block") --or something
maxDistance = 10

char.Torso.Changed:connect(function(prop)
    if prop == "CFrame" then
        local dis = (block.Position-char.Torso.Position).magnitude
        if dis <= maxDistance then

        end
    end
end)

Well great! Now we have the distance between the player and the block.

Next, we need to get the gui visible.

--in a localScript
player = game.Players.LocalPlayer
if not player.Character then player.CharacterAdded:wait() end
char = player.Character
block = game.Workspace:WaitForChild("Block") --or something
maxDistance = 10
gui = --something

char.Torso.Changed:connect(function(prop)
    if prop == "CFrame" then
        local dis = (block.Position-char.Torso.Position).magnitude
        if dis <= maxDistance then
            gui.Visible = true
        else
            gui.Visible = false
        end
    end
end)

Perfect! Now for the space press.

--in a localScript
player = game.Players.LocalPlayer
if not player.Character then player.CharacterAdded:wait() end
char = player.Character
block = game.Workspace:WaitForChild("Block") --or something
maxDistance = 10
gui = --something

char.Torso.Changed:connect(function(prop)
    if prop == "CFrame" then
        local dis = (block.Position-char.Torso.Position).magnitude
        if dis <= maxDistance then
            gui.Visible = true
        end
    end
end)

game:GetService("UserInputService").InputBegan:connect(function(key,gameThing)
    if key.KeyCode == Enum.KeyCode.Space and not gameThing and gui.Visible then
        --do stuff
    end
end)

Perfect! UserInputService FTW! And magnitude

0
Pretty good, but may need some explaining, since, like he said, he is ignorant of how this works. TheDeadlyPanther 2460 — 8y
0
Wow, I wasn't expecting such an in depth response. Thank you very much!! ShiningWindow 127 — 8y
Ad

Answer this question