Perimeter issues

I am having trouble sending over perimeters. In this script, I use the collection service to detect when a part has been touched. Each part has a unique Gui, but I am having trouble detecting which part the player touches. Here is my current code.

I have tried doing v.Touched:Connect(SetUp(v.Parent, v.Name)), and creating a variable in the SetUp function called TouchedPart, and doing local TouchedPart = NPC:FindFirstChild(TouchedPartName), but it always has an error, mentioning something about UserData

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.Character:Wait()

local CollectionService = game:GetService("CollectionService")

local NPCFolder = game.Workspace.ShopNPCs

local GlobalTouchedPart

local PlayerGui = Player:WaitForChild("PlayerGui")
local UserInputService = game:GetService("UserInputService")
local NPCGui = PlayerGui:FindFirstChild("NPCGui")

local CurrentCamera = game.Workspace.CurrentCamera

local BlurEffect = Instance.new("BlurEffect", CurrentCamera)
BlurEffect.Size = 0
BlurEffect.Name = "ShopBlurEffect"

local OpenPosition = UDim2.new(0,0,0,0)
local ClosePosition = UDim2.new(-1, 0,0, 0)

local Toggle = false
local TouchToggle

local PlayerWalkSpeed = 16
local PlayerJumpPower = 50


local function OpenShop(Frame)
	if Frame.Visible == false then
		local HolderFrame = Frame:FindFirstChildOfClass("Frame")
		if HolderFrame.Position == ClosePosition then
			repeat
				wait(0.1)
				BlurEffect.Size = BlurEffect.Size + 1
			until BlurEffect.Size>= 10
			BlurEffect.Size = 10
			Frame.Visible = true
			Frame.BackgroundTransparency = 1
			repeat 
				wait()
				Frame.BackgroundTransparency = Frame.BackgroundTransparency - 0.05
			until Frame.BackgroundTransparency <= 0.3
			Frame.BackgroundTransparency = 0.3
			HolderFrame:TweenPosition(OpenPosition, "Out", "Linear", 1)
		end
	end
end

local function DisableMove()
	local PlayerCharacter = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.Character:Wait()
	local Humanoid = PlayerCharacter:FindFirstChild("Humanoid")
	Humanoid.WalkSpeed = 0
	Humanoid.JumpPower = 0
	local HumanoidRootPart = PlayerCharacter:FindFirstChild("HumanoidRootPart")
	HumanoidRootPart.Anchored = true
end

local function EnableMove()
	local PlayerCharacter = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.Character:Wait()
	local Humanoid = PlayerCharacter:FindFirstChild("Humanoid")
	Humanoid.WalkSpeed = PlayerWalkSpeed
	Humanoid.JumpPower = PlayerJumpPower
	local HumanoidRootPart = PlayerCharacter:FindFirstChild("HumanoidRootPart")
	HumanoidRootPart.Anchored = false
end

local function OpenInteractiveLabel()
	local InteractiveLabel = NPCGui:FindFirstChild("EInteractiveLabel")
	InteractiveLabel.Visible = true
end

local function CloseShop(Frame)
if Frame.Visible == true then
		local HolderFrame = Frame:FindFirstChildOfClass("Frame")
		if HolderFrame.Position == OpenPosition then
			HolderFrame:TweenPosition(ClosePosition, "Out", "Linear", 1)
			wait(1)
			repeat 
				wait()
				Frame.BackgroundTransparency = Frame.BackgroundTransparency + 0.05
			until Frame.BackgroundTransparency >= 1
			Frame.BackgroundTransparency = 1
			BlurEffect = CurrentCamera.ShopBlurEffect
			repeat
				wait(0.1)
				BlurEffect.Size = BlurEffect.Size - 1
			until BlurEffect.Size <= 0
			BlurEffect.Size = 0
			Frame.Visible = false
		end
	end
end

local function CloseInteractiveLabel()
	local InteractiveLabel = NPCGui:FindFirstChild("EInteractiveLabel")
	InteractiveLabel.Visible = false	
end


local function CheckKey(Input, GameProccessed)
	if Toggle == true then
		if Input.KeyCode == Enum.KeyCode.E then
			OpenShop(GlobalTouchedPart:FindFirstChildOfClass("Frame"))	
			CloseInteractiveLabel()
			--DisableMove()
		end
	end
end

local function SetUp(Hit, NPC, TouchedPartName)
	if Toggle == false then		
		Toggle = true
		local TouchedPart = NPC[TouchedPartName]
		GlobalTouchedPart = TouchedPart
		OpenInteractiveLabel()
	end
end

local function CloseUp(Hit, NPC, TouchedPartName)
	if Toggle == true then
		Toggle = false
		local TouchedPart = NPC[TouchedPartName]
		CloseInteractiveLabel()	
		--EnableMove()
		if TouchedPart:IsA("UnionOperation") or TouchedPart:IsA("Part") then
			CloseShop(TouchedPart:FindFirstChildOfClass("Frame"))	
		end
		if TouchedPart:IsA("TextButton")  or TouchedPart:IsA("ImageButton") then
			CloseShop(TouchedPart.Parent.Parent)	
		end	
	end
end

local function SetUpTables()
	for i,v in pairs(NPCFolder:GetChildren()) do
		CollectionService:AddTag(v:FindFirstChild("HitBox"), "HitBoxCollectionService")
		CollectionService:AddTag(v:FindFirstChild("EndHitBox"), "EndHitBoxCollectionService")
		CollectionService:AddTag(v:FindFirstChild("HitBox"):FindFirstChildOfClass("Frame"):FindFirstChildOfClass("Frame"):FindFirstChild("CloseButton"), "CloseButtonCollectionService")
	end	
end

local function ConnectTables()
	local TaggedHitBoxes = CollectionService:GetTagged("HitBoxCollectionService")
	local TaggedEndHitBoxes = CollectionService:GetTagged("EndHitBoxCollectionService")
	local TaggedCloseButton = CollectionService:GetTagged("CloseButtonCollectionService")
	
	for i,v in pairs(TaggedHitBoxes) do	
		v.Touched:Connect(SetUp(v.Parent, v.Name))
	end
	for i,v in pairs(TaggedEndHitBoxes) do	
		v.Touched:Connect(CloseUp(v.Parent, v.Name))
	end
	for i,v in pairs(TaggedCloseButton) do
		v.MouseButton1Click:Connect(CloseUp(v.Parent, v.Name))
	end
end



SetUpTables()
ConnectTables()

UserInputService.InputBegan:Connect(CheckKey)

Thanks for all the help!

1 Like

Have you tried

v.Touched:Connect(function()
    Setup(v.Parent, v.Name)
end)

I don’t think you’re able to pass parameters to a function the way you’re attempting to.

Edit: You don’t need to include the parameters in the function() part. Wouldn’t hurt though

1 Like

A touched function calls 1 variable, Hit. Right now I’m trying to send over v as a perimeter, to call that local UI unique to itself.

Yes, and if you want to pass parameters besides hit to a function on the touched event you should do it the way I described

Yup! I agree. I am testing it out right now.

1 Like

You have several problems. I recommend understanding scope of functions and also functions and arguments.

Thanks! I actually do know these concepts, however, some of my code in these scripts are a bit outdated from a ton of testing, and refer other things that may not exist, and similiar problems like that. Right now, If I cant get the SetUp function to work, I would start working on updating the other code to fit this code.

1 Like