Help stop GetChildren Loop a Proximity.Triggered script

hello, i have a script where when a player triggers a proximity prompt it gets the players children and makes CanCollide off which works, but it also makes the script repeat over and over, how do i stop the looping?


ProximityPromptService.PromptTriggered:Connect(function(prompt, player)

			local PlayerModel = Player:GetChildren()

			for i = 1, #PlayerModel do
				if PlayerModel[i]:IsA("MeshPart") or PlayerModel[i]:IsA("Part") then
					
			PlayerModel.CanCollide = false	

		end
	end
end)

the script has more than this but i deleted the parts out, other parts of script was just playing a animation

2 Likes
ProximityPromptService.PromptTriggered:Connect(function(prompt, Player)
	local PlayerModel = Player.Character:GetChildren()

	for i, child in pairs(PlayerModel) do
		if child:IsA("BasePart") then
			child.CanCollide = false
		end
	end
end)

fixed your script a bit here ^
ProximityPromptService.PromptTriggered is fired whenever any proximity prompt is triggered and in order for the code above not to repeat you need to add an if statement that checks whether the tirggered prompt is the needed one
e.g

if prompt ~= TheNeededPrompt then
	return
end

if triggered prompt isnt equal to needed prompt then return which stops the function ^

a better way to do this is just by connecting this function directly to the needed prompt

local TheNeededPrompt = workspace.Part.PromixityPrompt -- for example

TheNeededPrompt.Triggered:Connect(function(Player)
	local PlayerModel = Player.Character:GetChildren()

	for i, child in pairs(PlayerModel) do
		if child:IsA("BasePart") then
			child.CanCollide = false
		end
	end
end)
2 Likes

still loops for some reason, heres full script just in case


local ProximityPromptService = game:GetService("ProximityPromptService")
local TS = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.3)
local Event = game.ReplicatedStorage.GameEvents.Hide

ProximityPromptService.PromptTriggered:Connect(function(prompt, player)
	if prompt.Parent.Parent.Parent.Name == "HidingSpots" then
		if prompt.Parent.Parent.Name == "Cabinet" then
			
			local Player = player.Character
			local Root = Player:WaitForChild("HumanoidRootPart")
			local Humanoid = Player:WaitForChild("Humanoid")
			local Cabinet = prompt.Parent.Parent
			local AnimationController = Cabinet.AnimationController
			local TeleportTo = Cabinet:WaitForChild("TeleportTo")
			
			local Open = Cabinet:WaitForChild("Open")
			local OpenAnim = AnimationController.Animator:LoadAnimation(Open)
			
			local PlayerOpen = Cabinet:WaitForChild("PlayerOpen")
			local PlayerOpenAnimation = Humanoid:LoadAnimation(PlayerOpen)
			PlayerOpenAnimation.Priority = Enum.AnimationPriority.Action
			
			local TeleportToFront = TS:Create(Root, TweenInfo.new(.070, Enum.EasingStyle.Linear), {CFrame = TeleportTo.CFrame})
			local FaceTowardsFront = TS:Create(Root, TweenInfo.new(.070, Enum.EasingStyle.Linear), {Orientation = TeleportTo.Orientation})
			
			local PlayerModel = Player:GetChildren()

			for i, child in pairs(PlayerModel) do
				if child:IsA("BasePart") or child:IsA("MeshPart")  then
					child.CanCollide = false
					
			Event:FireClient(player,prompt)
			Root.Anchored = true
			TeleportToFront:Play()
			FaceTowardsFront:Play()
			TeleportToFront.Completed:Wait()
			PlayerOpenAnimation:Play()
			OpenAnim:Play()
			wait(1.3)
			PlayerOpenAnimation:AdjustSpeed(0)
					
				end
			end
		end
	end
end)

2 Likes

image
are you sure you meant to keep this block of code inside for loop?

2 Likes

oh yeah ur right lol, thanks filllerrrrrrrrrrr

1 Like

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