Cant check if player has left a zone and re-entered it using GetPartsInPart()

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

2 Likes
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
3 Likes

what did you exactly do here??

2 Likes

A new tween is created everytime instead of using the same one (In your old script, you create a tween variable and try to reuse it)

2 Likes

ooh alright, but also when i tried to copy ur script to see what exactly changed, i runned it and it made the area a default part
image

2 Likes

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
2 Likes

alright thanks!, but theres a problem that it only repeats twice now, like, the first time i re-enter it pops back up but the third time it dissapears

now i tried it again and doesnt work at all :sob:

1 Like

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)
2 Likes

it works perfectly now, thanks! although i think ill have to research on how to use RunService cause i’ve never used it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.