How can I let a player open a GUI when it's only in the proximity that I set or when touching a block that has "can collide" off.
So basically it wouldn't let players press the key "E" to open a GUI when it's not in a certain area.
How can I let a player open a GUI when it's only in the proximity that I set or when touching a block that has "can collide" off.
So basically it wouldn't let players press the key "E" to open a GUI when it's not in a certain area.
So since you didn't attempt this, I'm just going to explain what you need to look into and some pseudocode of how to do it.
You need to use the UserInputService in a local script inside starter scripts to make an anonymous function that detects if the player presses E when they are not typing in chat. Once you have that established, you will need to check if the player is within your set proximity:
Use (PlayerPosition-partPosition).Magnitude to get the distance of the player to the part and check if the proximity is greater than that, if so, then you would enable the GUI.
Also, if you want them to be able to close the GUI, have an if then else end block handling the GUI enabling like this:
--THIS IS PSEUDOCODE, IT WILL NOT WORK
function detectKeypress()
local GUI = yourGUI
if not GUI.enabled and proximity>distance then
GUI.enabled = true
else
GUI.enabled = false
end
end
for your solution i have made a model which i will link you in case you cant figure out how to do this but i also want you to learn a little even tho that sounds stupid it will help you in scripting. so to start you want to make a part in workspace and make it as big as you want and consider making it about 6 studs tall anchor, and then Cancollide = false. next make it invisible by making transparency 1. then insert a server script into it and type in this code:
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = workspace[hit.Parent.Name]
local player = game.Players:GetPlayerFromCharacter(plr)
player.guis.egui.Value = true
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = workspace[hit.Parent.Name]
local player = game.Players:GetPlayerFromCharacter(plr)
player.guis.egui.Value = false
player.PlayerGui.egui.Frame.Visible = false --change egui.Frame to your screenGui name and frame name
end
end)
what this code does is when the player touches the part it gives it the persons boolvalue a true value(which i will get to in a second). and the bool value becomes false when the player leaves the area or untouches it or isnt touching it anymore.
next you want to insert a script into serverscriptservice with the following code:
game.Players.PlayerAdded:connect(function(player)
local guis = Instance.new("Folder")
guis.Name = "guis"
guis.Parent = player
local egui = Instance.new("BoolValue", guis)
egui.Name = "egui"
end)
this script creates a bool value which when its true it lets the person open the gui and when its false it doesnt let the person open the gui, also it only becomes true when the person is touching the part we scripted first.
next insert a local script into starterPlayer, StarterPlayerScript with this code:
local plr = game.Players.LocalPlayer
local char = plr.Character
game:GetService("UserInputService").InputBegan:Connect(function(input, event)
if input.KeyCode == Enum.KeyCode.E and plr.guis.egui.Value == true then
if script.Parent.Parent.PlayerGui.egui.Frame.Visible == false then--change egui.Frame to your screenGui name and frame name on all of these lines that make the gui visible or uninvisible
script.Parent.Parent.PlayerGui.egui.Frame.Visible = true
elseif script.Parent.Parent.PlayerGui.egui.Frame.Visible == true then
script.Parent.Parent.PlayerGui.egui.Frame.Visible = false
end
end
end)
this code is played when the person is touching the part, has the boolvalue on true and presses E. this script then gets the gui and makes it either visible or invisible.
hope this helped you :)
also here is the link if this was too confusing / / / / /
holy crap this took me an hour to make
Thanks but the model is currently private can you open it so I can test it?
oh sorry
I made it public when you test it can you tell me if it worked or not
just use magnitude, or place a touched object around the block
this is for the touched object (Server Script)
script.Parent.Touched:Connect(function(hit)
local player = hit.Parent:GetPlayerFromCharacter()
player.PlayerGui. <-- insert userinput script.Disabled = false
end)
script.Parent.TouchEnded:Connect(function(hit)
local player = hit.Parent:GetPlayerFromCharacter()
player.PlayerGui. <-- insert userinput script.Disabled = true
end)
Key code script vvv (LocalScript)
local UserInput = game:GetService("UserInputService")
UserInput.InputBegan:Connect(function(input)
if input.Keycode == Enum.KeyCode.E then
game.Players.Localplayer.PlayerGui. <-- put Gui to make visible.Visible = true
end)
haven't tested this btw
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
Im sorry what's a PSEUDOCODE?
"pseudo" means fake, so pseudocode means fake code. It's a common term you will hear a lot in the future if you plan to continue scripting and all it really is, is a way for people to explain how code will be set up without actually coding everything for you.
The literal definition is: a notation resembling a simplified programming language, used in program design.
Also, since this does answer your question, mark it as the accepted answer.