im working on a system in which, if u enter a zone, a GUI pops up (using tween service) telling you that you have entered that area, then goes outside the screen, and it doesnt appear again unless you leave the zone, then re-enter
it works as intended, u enter it and it pops up but doesnt redo it if you leave the zone then re-enter
local script is located in starterGui
local area = Instance.new("Part")
area.Parent = workspace
area.Position = Vector3.new(30,5,30)
area.Size = Vector3.new(20,10,20)
area.BrickColor = BrickColor.new("Bright green")
area.CanCollide = false
area.Anchored = true
local label = script.Parent.TextLabel
local plr = game.Players.LocalPlayer
local root = plr.Character:FindFirstChild("HumanoidRootPart")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,true)
local goal = {
Position = UDim2.new(0.5,0,0.5,0)
}
local tween = tweenService:Create(label,tweenInfo,goal)
local tweenCanBeRepeated = true
while true do
local partstable = workspace:GetPartsInPart(area)
print(partstable)
for i, parts in partstable do
if table.find(partstable, root) and tweenCanBeRepeated == true then
tween:Play()
tweenCanBeRepeated = false
print("player is in area")
elseif not table.find(partstable,root) and tweenCanBeRepeated == false then
tweenCanBeRepeated = true
print("player is no longer in the area")
end
end
task.wait(1)
end
local area = Instance.new("Part")
area.Parent = workspace
area.CFrame = Vector3.new(30,5,30)
area.Size = Vector3.new(20,10,20)
area.BrickColor = BrickColor.new("Bright green")
area.CanCollide = false
area.Anchored = true
local label = script.Parent.TextLabel
local plr = game.Players.LocalPlayer
local root = plr.Character:FindFirstChild("HumanoidRootPart")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,true)
local goal = {
Position = UDim2.new(0.5,0,0.5,0)
}
local tweenCanBeRepeated = true
while true do
local partstable = workspace:GetPartsInPart(area)
print(partstable)
for i, parts in partstable do
if table.find(partstable, root) and tweenCanBeRepeated == true then
tweenService:Create(label,tweenInfo,goal):Play()
tweenCanBeRepeated = false
print("player is in area")
elseif not table.find(partstable,root) and tweenCanBeRepeated == false then
tweenCanBeRepeated = true
print("player is no longer in the area")
end
end
task.wait(1)
end
Oh I had a mistake, at the third line I try to set area.CFrame = Vector3.new(30,5,30) but pass in a Vector3 value, change area.CFrame to area.Position. My bad!
Here’s the corrected script:
local area = Instance.new("Part")
area.Parent = workspace
area.Position = Vector3.new(30,5,30)
area.Size = Vector3.new(20,10,20)
area.BrickColor = BrickColor.new("Bright green")
area.CanCollide = false
area.Anchored = true
local label = script.Parent.TextLabel
local plr = game.Players.LocalPlayer
local root = plr.Character:FindFirstChild("HumanoidRootPart")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,true)
local goal = {
Position = UDim2.new(0.5,0,0.5,0)
}
local tweenCanBeRepeated = true
while true do
local partstable = workspace:GetPartsInPart(area)
print(partstable)
for i, parts in partstable do
if table.find(partstable, root) and tweenCanBeRepeated == true then
tweenService:Create(label,tweenInfo,goal):Play()
tweenCanBeRepeated = false
print("player is in area")
elseif not table.find(partstable,root) and tweenCanBeRepeated == false then
tweenCanBeRepeated = true
print("player is no longer in the area")
end
end
task.wait(1)
end
This is because the script to update if the tween can play again or not only runs when it detects a part inside the area, so it will ONLY allow the tween to play again when the player is touching the area but not with their HumanoidRootPart.
Try this script I made:
local area = Instance.new("Part")
area.Parent = workspace
area.Position = Vector3.new(30,5,30)
area.Size = Vector3.new(20,10,20)
area.BrickColor = BrickColor.new("Bright green")
area.CanCollide = false
area.Anchored = true
local label = script.Parent.TextLabel
local plr = game.Players.LocalPlayer
local root = plr.Character:FindFirstChild("HumanoidRootPart")
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Back,Enum.EasingDirection.Out,0,true)
local goal = {
Position = UDim2.new(0.5,0,0.5,0)
}
local currentTween = tweenService:Create(label,tweenInfo,goal)--Track current tween so the tween will only play again if the old one has finished playing
local tweenCanBeRepeated = true
area.Touched:Connect(function(hit)
if hit==root and tweenCanBeRepeated and currentTween.PlaybackState ~= Enum.PlaybackState.Playing then
currentTween = tweenService:Create(label,tweenInfo,goal)
currentTween:Play()
--Update current tween variable and play
tweenCanBeRepeated = false
local checkIfStillIn
checkIfStillIn = runService.Heartbeat:Connect(function()--Heartbeat runs every frame, so its a very precise check
if table.find(workspace:GetPartsInPart(area),root) then
--Player is still inside!
return
end
--Player wasn't inside, tween can play again
tweenCanBeRepeated = true
checkIfStillIn:Disconnect()
end)
end
end)