Hi i am creating a game with interact system with ScreenGui instead of proxomity prompt.
i want to set Frame.visible = false as soon as the character leaves the zone but i can’t find a solution or i am just dumb
my code
local Zone = script.Parent
local NPC = script.Parent.Parent
local function entered(Player,boolValue)
local interactUI:ScreenGui = Player.PlayerGui.Interact
local Frame:Frame = interactUI.Frame
local TextLabel = Frame.frame:WaitForChild("TextLabel")
Frame.Visible = boolValue
TextLabel.Text = NPC.Text.Value
end
Zone.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
entered(Player,true)
end
end
end)
i tried caculating distance between npc and player but didnot work and i watched youtube but most of them used proximity prompt
Why exactly do you not want to use proximity prompts? They would work the best for what you need. Other than that you could start checking the distance from that zone once a player touches it.
(zone.Position - player.Position).Magnitude
Just have that in a loop and once player is too far away from the zone you can disable the ui. Also make sure to make it so when player touches the zone once they cant touch it again until they leave it, you can add players to the table and check if they are in it, if not then add them into it after they touch the zone if its server sided or just have a debounce if its client sided.
local Zone = script.Parent
local NPC = script.Parent.Parent
local function entered(Player,boolValue: bool)
local interactUI:ScreenGui = Player.PlayerGui:WaitForChild("Interact")
local Frame:Frame = interactUI.Frame
local TextLabel = Frame.frame:WaitForChild("TextLabel")
Frame.Visible = boolValue
TextLabel.Text = NPC.Text.Value
end
Zone.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player then
entered(Player,true)
end
end)
Why exactly do you not want to use proximity prompts?
because i wanted something like in this image
i did try something like you said before
for _,Zone in pairs(workspace.interactableZone:GetChildren()) do
local Mag = (Zone.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
if Mag <= 10 then
Frame.Visible = true
end
end
You can achieve that with prompts. Just make sure prompts have their Style set to Custom so they dont show when you get close to them, then have a local script with this code:
local promptservice = game:GetService("ProximityPromptService")
local text = script.Parent.interact
promptservice.PromptShown:Connect(function(prompt)
text.Text = prompt.ActionText
text.Visible = true
end)
promptservice.PromptHidden:Connect(function()
text.Text = ""
text.Visible = false
end)