Hello! I made a box where if you walk into it, it shows the gui and displays the name of whatever name you want it to say and then it slowly fades away. There’s something I want to change about it though and I don’t know how to do it. When you go out of the box, the gui doesn’t disappear until it slowly fades away and when you go back in the box, it doesn’t appear again. I am very new to scripting so that’s why I’m asking here. Thank you!
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local title = script.Parent.Title
local area = game.Workspace.Area
local toggle = false
while true do wait(1)
for i, section in pairs(area:GetChildren()) do
local pos1,pos2 = (section.Position - (section.Size / 2)),(section.Position + (section.Size / 2))
local region = Region3.new(pos1,pos2)
local playersFound = game.Workspace:FindPartsInRegion3(region)
for i, playersInArea in pairs(playersFound) do
if playersInArea:FindFirstAncestor(player.Name) then
toggle = true
if title.Text == section.Name then
--[[ Nothing happens]]
else
title.Text = section.Name
title.Parent.Enabled = true
for i = 1,0,-0.1 do
title.TextTransparency = i
wait(0.2)
if i == 0 then
break
end
end
wait(1)
for i = 0,1,0.1 do
title.TextTransparency = i
wait(0.2)
if i == 1 then
break
end
end
end
else
toggle = false
end
end
end
end
so the problem with your code rn is that it can only detect wether the player is inside or outside the box once the for loops are finished so to fix this you could use Co-routines: coroutine or you could use tweening: TweenService to smoothly transition the transparency in and out (you set up a tween then do tween:play and the code will move on while its running the transition) additionally tweening would be good as you can set it so that it tweens to the goal transparency then tweens back again to get that fading in and out effect. so if you decide to tween then you can add to your code
if playersInArea:FindFirstAncestor(player.Name) then
...your code...
else
title.Parent.Enabled = false
end
if playersInArea:FindFirstAncestor(player.Name) then
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local title = script.Parent.Title
local area = game.Workspace.Area
local toggle = false
while true do wait(1)
for i, section in pairs(area:GetChildren()) do
local pos1,pos2 = (section.Position - (section.Size / 2)),(section.Position + (section.Size / 2))
local region = Region3.new(pos1,pos2)
local playersFound = game.Workspace:FindPartsInRegion3(region)
for i, playersInArea in pairs(playersFound) do
if playersInArea:FindFirstAncestor(player.Name) then
toggle = true
if title.Text == section.Name then
--[[ Nothing happens]]
else
title.Text = section.Name
title.Parent.Enabled = true
for i = 1,0,-0.1 do
title.TextTransparency = i
wait(0.2)
if i == 0 then
break
end
end
wait(1)
for i = 0,1,0.1 do
title.TextTransparency = i
wait(0.2)
if i == 1 then
break
end
end
end
else
toggle = false
end
end
end
end
else
title.Parent.Enabled = false
end
That is the code that is in right now, tell me if I did anything wrong
As I said, I’m new to scripting and I have no idea what that even is. Could you explain how to fix it the way I want it or write it out the way I want it maybe? Thank you!
Alright, could you show me how and where to add that in my code? I don’t know how to use .Touched or .TouchEnded events cause I am very, very new to scripting and by that I mean like I just started days ago.
local ZonePlus = require(game:GetService("ReplicatedStorage").ZonePlus) -- The zoneplus module
local fol = Instance.new("Folder")
local area = workspace.Area
area.Parent = fol
fol.Parent = workspace
local zone = ZonePlus.new(fol)
This would create a Zone.
To detect if a player goes in the zone you do
.Touched and .TouchEnded will be much harder since they only fire when the player hits the side of the box. Once the player is inside the box it will fire TouchEnded since the player is no longer touching the side of the box. So I would go with @nonamehd24 answer.
Ok well is there any other way to do it than both of those? I’m not using ZonePlus because I don’t want to create a new zone and I wont use the touched stuff cause apparently it doesn’t work. All my point is that I want it so when I leave the zone, the gui will disappear, and when i enter the zone the gui will pop back up. Maybe some sort of loop or something repeating will work but I’m not sure.
You should create a hitbox part to detect collision with .Touched event. this is an example
of how you could do that
local hitbox = workspace.Area.hitbox
local plrsInPart = {}
local function plrInPart(part)
local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
if plr then
return plr
end
end
hitbox.Touched:Connect(function(part)
local plr = plrInPart(part)
if plr and not plrsInPart[plr.Name] then
table.insert(plrsInPart, plr.Name)
-- Code logic
end
end)
hitbox.TouchEnded:Connect(function(part)
local plr = plrInPart(part)
if plr and plrsInPart[plr.Name] then
table.remove(plrsInPart, plr.Name)
-- Code logic
end
end)
There are others though they aren’t the most efficient, but they should do the trick. You could add an event for .CharacterAdded and calculate if the position of the Characters PrimaryPart is inside the the box using some calculation methods such as if primarypart’s position is inside the position + size it would fire such function and add a debounce so it fires once and when it goes out it would fire another function. I can show you an example if you wanted to. I’m gonna go make something in roblox studio I’ll post my answer when it will work and explain the code.
I inserted this in the top of my main script in the area gui where the script is and it kinda works but when i enter it says hitbox then when i re enter it says area and then that cycle just keeps going basically. Could you reply with a correct way to put this in my code with only using 1 part and not 2? Thank you for your help!
This is my example: (edit: I forgot I didn’t need any coroutines)
local function isInside(position1, position2, size2)
return (position1.X >= position2.X - size2.X / 2 and position1.X <= position2.X + size2.X / 2) and -- X Axis
(position1.Y >= position2.Y - size2.Y / 2 and position1.Y <= position2.Y + size2.Y / 2) and -- Y Axis
(position1.Z >= position2.Z - size2.Z / 2 and position1.Z <= position2.Z + size2.Z / 2)
end
local hitbox = game:GetService("Workspace").hitboxPart -- CHANGE THIS TO YOUR PART LOCATION
game:GetService("Players").PlayerAdded:Connect(
function(plr)
local gui = plr.PlayerGui:WaitForChild("GuiName")
gui.Enabled = false
gui.Parent = plr.PlayerGui
plr.CharacterAdded:Connect(
function(char)
local debounce = false
while char do
wait()
if isInside(char.PrimaryPart.Position, hitbox.Position, hitbox.Size) and not debounce then
print("Player is inside")
-- Add gui to the screen of the player
gui.Enabled = true
debounce = true
elseif debounce and not isInside(char.PrimaryPart.Position, hitbox.Position, hitbox.Size) then
print("Player is outside")
-- Remove gui from the screen of the player since the player is outside of the box
gui.Enabled = false
debounce = false
end
end
end
)
end
)
I’m not adding 2 parts. I’m telling you I’m keeping in 1 part, not 2. All I need is a way to make the gui disappear when the player leaves the box and when they enter the box again I want it to show up again. I don’t want to create anything new unless it’s inside the script.