In Pairs() Only Runs Once. Please Help!

Howdy! I have an issue with my code. I am trying to make the player become invisible every time they sit down using a proximity prompt. However, the code will only run once! Please help me!

local proximityPrompt = script.Parent
local seat = proximityPrompt.Parent

local function prompt(player)
	seat:Sit(player.Character.Humanoid)
	for _, Child in ipairs(player.Character:GetChildren()) do
		if Child:IsA("Part")then
			print(Child)
			Child.Transparency = 1
	for _, Childd in ipairs(player.Character:GetChildren()) do
		if Childd:IsA("Accessory")then
			print(Childd)
			Childd.Handle.Transparency = 1
				end
			end
		end
	end
end	

proximityPrompt.Triggered:Connect(prompt)

The issue is not the sitting part, that works fine. The issue is making them go invisible.

I have read countless forums, to no avail however. Can someone please tell me how I can fix it? My goal is for the proximity prompt to fire every time and make the character model invisible. The script is located underneath the Proximity Prompt and is a client script.

EDIT: Decided to specify, it prints the child’s name every time, but does not make them transparent every time. Only the first time the proximity prompt is fired.

(apologize if my code is messy, i’m fairly new lol)

1 Like

Here’s the fixed code:

local proximityPrompt = script.Parent
local seat = proximityPrompt.Parent

local function onTrigger(user: Player)
	seat:Sit(user.Character.Humanoid)
	
	for _, object in user.Character:GetDescendants() do
		if object:IsA("BasePart") then
			object.Transparency = 1
		end
	end
end	

proximityPrompt.Triggered:Connect(onTrigger)

Your main issue was your for loops. They seem to be separate, but the second one is actually inside your Child:IsA("Part") statement, which means it will only run that loop for each Part.

This code still might not do what you want because it wouldn’t make every type of object “invisible”. This requires accounting for every type of visible object. Try to get help from AI on this one!

1 Like

local proximityPrompt = script.Parent
local seat = proximityPrompt.Parent

local function prompt(player)
	seat:Sit(player.Character.Humanoid)
	for _, Child in ipairs(player.Character:GetChildren()) do
		if Child:IsA("BasePart")then
			print(Child)
			Child.Transparency = 1
			for _, Childd in ipairs(player.Character:GetChildren()) do
				if Childd:IsA("Accessory")then
					print(Childd)
					Childd.Handle.Transparency = 1
				end
			end
		end
	end
end	

proximityPrompt.Triggered:Connect(prompt)

Characters’ body parts are MeshParts, which are not considered specifically Parts. They are BaseParts though (more general class including meshes, wedges, accessories, etc), so changing IsA(“Part”) to IsA(“BasePart”) should work.

Yeah yours is actually more efficient

Assuming this is a LocalScript, these scripts don’t run under workspace. A better approach would be to add a LocalScript to StarterPlayerScripts.
image

Then you can use this code and it should work!
I know it may look complex, but this makes sure there’s no bugs. It also supports multiple seats. If that’s what you need.

Just using this code won’t work, you need to add a tag named Seat to each seat you want the code to work for. You can do this in the properties widget.

local CollectionService = game:GetService("CollectionService")
local SEAT_TAG_NAME = "Seat"

local function onProximityPromptTriggered(player, seat)
	local character = player.Character

	if not character then
		return
	end

	local head = character:FindFirstChild("Head")
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	if not humanoid then
		return
	end

	seat:Sit(humanoid)

	if head then
		head.face.Transparency = 1
	end

	for _, descendant in character:GetDescendants() do
		if descendant:IsA("BasePart")then
			descendant.Transparency = 1
		end
	end
end

local function onSeatTagged(seat)
	seat.ProximityPrompt.Triggered:Connect(function(player)
		onProximityPromptTriggered(player, seat)
	end)
end

for _, seat in CollectionService:GetTagged(SEAT_TAG_NAME) do
	task.spawn(onSeatTagged, seat)
end

CollectionService:GetInstanceAddedSignal(SEAT_TAG_NAME):Connect(onSeatTagged)

Let me know if you have any questions!

1 Like

i think youre overcomplicating it, the easiest way to make it run is to just change the runcontext to client

Still getting an issue with it not working the second time.

This happened to work for me. Thank you.

1 Like

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