Touch script isn't working, using loops

My script doesnt work when I touch the part

here is my code:

local checkpointsFolder = game.Workspace.CheckpointsFolder
print("Script Running")

for i, v in pairs(checkpointsFolder:GetChildren()) do
	v.Touched:Connect(function()
		print("hi")
	end)
end
2 Likes

Have you checked if there is other instances than parts in the folder (like script or models) ? This will cause an error

2 Likes

Try adding a IsA method when checking for each children, to check whether the child is a part.

So, the script should be something like this:

for i, v in pairs (checkpointsFolder:GetChildren()) do
	if v.IsA(“Part”) then -- checking whether the child is a part
	 v.Touched:Connect(function()
		print("hi")
	 end)
	end
end
2 Likes

Are the parts being added after the script runs?

2 Likes

the script runs only when the player touches the part.

also the parts arent added, they are already there. in the workspace

1 Like

Nothing but spawnpoints. Just parts…

1 Like

Also the local script is in starter player, Is that correct?

EDIT: it only works in starter player after I play the game, then reset then it works. So then I tried to add a 5 second wait at the first line of the script cause maybe something is loading in faster than its supposed to, apparently it works now.

Something is loading in too fast. What would it be?

EDIT: I added a simple wait() at the very beginning and now everything works

EDIT: NOW WHEN I ADD SOUND EVERYTHING BREAKS AUGHH :sob:

1 Like

The reason is most likely because the folder hasn’t loaded in, so use WaitForChild() when referencing the folder in your variable.

So like workspace:WaitForChild(“CheckpointsFolder”)

1 Like

Instead of doing wait(), try doing game.Players.LocalPlayer.CharacterAdded:Wait(), this is a little better than wait() as that doesn’t insure the character has actually loaded in and it still might bug, also please mark your post as solved

1 Like

It doesn’t have anything to do with the character because he isn’t using any code that needs the character. The code is just running before the checkpoints folder is loading in.

1 Like

Okay, there is another problem, everything works fine but when I add Sound:Play() it would work the first few times then the script would just break and the other parts dont even turn green and or play any sound

here is my code:

game.StarterPlayer.StarterCharacterScripts:WaitForChild("Checkpoints")
game.StarterPlayer.StarterCharacterScripts.Checkpoints:WaitForChild("Sound")
game.Workspace:WaitForChild("CheckpointsFolder")
local sound = script:WaitForChild("Sound")
local checkpointsFolder = game.Workspace:WaitForChild("CheckpointsFolder")
local green = BrickColor.new("Lime green")

for i, v in pairs(checkpointsFolder:GetChildren()) do
	v.Touched:Connect(function()
		if v.BrickColor == green then
			print("Already green")
		else
			v.BrickColor = BrickColor.new("Lime green")
			sound:Play()
		end
	end)
end

1 Like

Try adding a print within the touched connection. Also the first 3 lines don’t do anything so just delete those.

2 Likes

it would work on the first two parts then just stop printing and working

its random, sometimes it works sometimes it doesnt

1 Like

ummm, weird…so uh. I just put the for i v in pairs thingy in a while loop and added a cooldown of one second and now everything works…

1 Like

Thats not good because then it would run the code multiple times when you touch it. Can I see how your checkpoints are placed?

1 Like

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