Size restricted area help

I’m trying to make an area that is restricted to being a certain size, I thought this should work?
Nothing happens below or above size 5.

local part = script.Parent 
local tpPart = part.Parent:WaitForChild("TP") 


part.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	
	if humanoid then
		local height = character:FindFirstChild("Height")

	
		if height and height.Value > 5 then
		
			character:SetPrimaryPartCFrame(tpPart.CFrame)

			
			local player = game.Players:GetPlayerFromCharacter(character)
			if player then
				player:SendNotification({
					Title = "Too Big!",
					Text = "You are too big to fit here! Teleported to a new location.",
					Duration = 3
				})
			end
		end
	end
end)

3 Likes

Look at your character in Studio. Do you see a property Height? Nope.
So when the script runs

local height = character:FindFirstChild("Height")

the value of height is effectively nil. You will need to manually calculate the size of the character yourself and create am IntValue in the character for this to work.

3 Likes

Correct me if I’m wrong, but I think I used the same method as a different script I have which shows the player height in a ui


local Player = game:GetService("Players").LocalPlayer
local Frame = script.Parent
local HealthBack = Frame:WaitForChild("HealthBack")
local Health = HealthBack:WaitForChild("Health")
local SizeBack = Frame:WaitForChild("SizeBack")
local Size = SizeBack:WaitForChild("Size")
local SizeLabel = SizeBack:WaitForChild("Label")
local TS = game:GetService("TweenService")
local Market = game:GetService("MarketplaceService")
local MaxHeight = 40

function Setup()
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Height = Character:WaitForChild("Height")
if Market:UserOwnsGamePassAsync(Player.UserId, 9228818) then
	MaxHeight = 65
end
if Humanoid then
	local function Update()
		if Humanoid.Health <= 25 then
			TS:Create(Health, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {BackgroundColor3 = Color3.fromRGB(255, 0, 0)}):Play()
		elseif Humanoid.Health <= 50 then
			TS:Create(Health, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {BackgroundColor3 = Color3.fromRGB(182, 170, 0)}):Play()
		elseif Humanoid.Health > 50 then
			TS:Create(Health, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {BackgroundColor3 = Color3.fromRGB(41, 117, 0)}):Play()
		end
		Health.Text = string.format("%s/%s", tostring(math.floor(Humanoid.Health)), tostring(Humanoid.MaxHealth))
		Health:TweenSize(UDim2.new((Humanoid.Health / Humanoid.MaxHealth) * 0.95, 0, Health.Size.Y.Scale, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
	end
	Humanoid:GetPropertyChangedSignal("Health"):Connect(Update)
	Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(Update)
	Update()
end
if Height then
	local function Update()	
		SizeLabel.Text = string.format("Size (%s)", tostring(Height.Value))
		if Height.Value < 0 or Height.Value > MaxHeight then
			return
		end
		Size:TweenSize(UDim2.new((Height.Value / MaxHeight) * 0.95, 0, Size.Size.Y.Scale, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 1, true)
	end
	Height:GetPropertyChangedSignal("Value"):Connect(Update)
	Update()
	end
end
Player.CharacterAdded:Connect(Setup)
Setup()

Does this work a different way?

Are you sure that isn’t using a calculated Height value from elsewhere? The clue is Height.Value. That tells me this is an inserted Value type.

3 Likes

In a different script this is what happens with Height

Function:

local function GetHeight(Character)
			if not Character then return end
			local Height = Character:FindFirstChild("Height")
			if not Height then return end
			return Height
		end

Usage:

local Height = GetHeight(character)
			if not Height then
				Height = Instance.new("NumberValue")
				Height.Name = "Height"
				Height.Value = 0
				Height.Parent = character
			end
1 Like

Just to be clear you’re 100% sure that this TP instance is not causing the script to yield forever?

Secondly, were you trying to do tpPart.Touched instead?

2 Likes

Yea, theres a part named TP inside of the parent part, and thats the one that needs to be ‘touched’ to get teleported back
image

1 Like

Hey kiri, it looks like you’re checking for touches on part, but you said you want it to happen when TP is touched.

Try changing this:

part.Touched:Connect(function(hit)

to this:

tpPart.Touched:Connect(function(hit)

Hopefully that fixes it!

2 Likes

I apologize for my miscommunication, I did mean that part is the one that needs to be touched. But even if changed, it still doesn’t work. I tried touching both parts and neither did anything

1 Like

Add print() statements at certain points, gives us more information on this issue.

print("Touched by:", hit.Name)
print("Height value:", height and height.Value)

 
Also Player:SendNotification is not a valid method.
The correct way would be doing this in the client:

game:GetService("StarterGui"):SetCore("SendNotification", {
    Title = "Too Big!",
    Text = "You are too big to fit here! Teleported to a new location.",
    Duration = 3
})
3 Likes

Hey, you were right about the infinite yield. I fixed it and it works awesome, it just doesnt give me a notification

local part = script.Parent 
local tpPart = part:WaitForChild("TP") 

part.Touched:Connect(function(hit)
	print("Touched by:", hit.Name) 
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		local height = character:FindFirstChild("Height")
		print("Height value:", height and height.Value) 

		if height and height.Value > 5 then
			character:SetPrimaryPartCFrame(tpPart.CFrame)

			local player = game.Players:GetPlayerFromCharacter(character)
			if player then
			
				game:GetService("StarterGui"):SetCore("SendNotification", {
					Title = "Too Big!",
					Text = "You are too big to fit here! Teleported to a new location.",
					Duration = 3
				})
			end
		end
	end
end)

2 Likes

I ended up using a left-behind Notify remote event that i forgot I had, thanks for the help, though! I appreciate it

1 Like

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