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

Text Gui and Keystrokes Within Distance of Block?

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

✓ Accepted answer

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

2 comments

0

Pretty good, but may need some explaining, since, like he said, he is ignorant of how this works.

0

Wow, I wasn't expecting such an in depth response. Thank you very much!!

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