I need a GUI to slide out when you stand in an area. It detects if the player is in an area and works unless I try and make it slide out from the left side.
here is my code
local players = game:GetService("Players")
local rser = game:GetService("RunService")
local plr = players.LocalPlayer
local gui = script.Parent
local areas = workspace:WaitForChild("MenuAreas")
local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist
rser.RenderStepped:Connect(function()
local character = plr.Character
if not character then return end
print("Stepped into part")
param.FilterDescendantsInstances = {character.PrimaryPart}
for _, area in ipairs(areas:GetChildren()) do
local overlap = workspace:GetPartsInPart(area, param)
if #overlap > 0 then
gui[area.Name].Visible = true
for i = 1, 10 do
wait(0.02)
gui[area.Name].Position = gui[area.Name].Position + UDim2.new(0.02, 0, 0, 0)
end
else
for i = 1, 10 do
wait(0.02)
gui[area.Name].Position = gui[area.Name].Position - UDim2.new(0.02, 0, 0, 0)
end
gui[area.name].Visible = false
end
end
end)
Quick fix you could do that might fix your script:
Switch the for loops you used to animate the GUI with Tween Animations (If you’re interested you can look into UI Animations).
Learning how to use Tweens to animate GUIs will save you a lot of time!
There’s some good Tween Tutorials on Youtube that can help you get the basics, this one for example.
Sorry for not giving any code examples!
I’m writing this post from my phone
In any case, I hope this helps or at least guides you towards fixing your issue.
You should never wait in a RenderStepped connection like this. It will create potential memory leaks and inconsistent behavior. Use TweenService, and avoid :TweenPosition and :TweenSize methods of GuiObjects.
local TweenService = game:GetService("TweenService")
local players = game:GetService("Players")
local rser = game:GetService("RunService")
local plr = players.LocalPlayer
local gui = script.Parent
local areas = workspace:WaitForChild("MenuAreas")
local tweenInInfo = TweenInfo.new(0.2)
local guiInDebounce = false
local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist
rser.RenderStepped:Connect(function()
if guiInDebounce then return end
guiInDebounce = true
local character = plr.Character
if not character then return end
print("Stepped into part")
param.FilterDescendantsInstances = {character.PrimaryPart}
for _, area in ipairs(areas:GetChildren()) do
if gui[area.Name].Visible then continue end
local overlap = workspace:GetPartsInPart(area, param)
if #overlap > 0 then
gui[area.Name].Visible = true
print("Showing GUI")
local tweenIn = TweenService:Create(gui[area.Name], tweenInInfo, {Position = UDim2.new(0.5, 0, 0.5, 0)}) --this udim will be the ending position
tweenIn:Play()
tweenIn.Completed:Wait()
guiInDebounce = false
else
guiInDebounce = true
print("Hidding GUI")
gui[area.Name].Visible = false
gui[area.Name].Position = UDim2.new(0.397, 0, 0, 0) --this will be the starting position
guiInDebounce = false
end
end
end)
but now it only triggers once and then when the player loads in
any solution?
If you want the gui to show when your standing in a zone, and close when you leave the zone, you should do this: Open the gui when zone.Touched is fired. Add a close button to the gui to let the user close it. Detecting when the zone is untouched will be a pain.
local TweenService = game:GetService("TweenService")
local players = game:GetService("Players")
local rser = game:GetService("RunService")
local plr = players.LocalPlayer
local gui = script.Parent
local areas = workspace:WaitForChild("MenuAreas")
local tweenInInfo = TweenInfo.new(0.3)
local guiInDebounce = false
local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist
rser.RenderStepped:Connect(function()
if guiInDebounce then return end
local character = plr.Character
if not character then return end
param.FilterDescendantsInstances = {character.PrimaryPart}
for _, area in ipairs(areas:GetChildren()) do
local overlap = workspace:GetPartsInPart(area, param)
if #overlap > 0 then
if gui[area.Name].Visible then continue end
guiInDebounce = true
gui[area.Name].Visible = true
local tweenIn = TweenService:Create(gui[area.Name], tweenInInfo, {Position = UDim2.new(0.5, 0, 0.5, 0)}) --this udim will be the ending position
tweenIn:Play()
tweenIn.Completed:Wait()
guiInDebounce = false
else
gui[area.Name].Visible = false
gui[area.Name].Position = UDim2.new(-10, 0, 0, 0) --this will be the starting position
end
end
end)
This is very inefficient though. If you still want to go with this, at least use .Heartbeat instead of .RenderStepped. I suggest trying what I said before.