For loop not working

  1. What do you want to achieve? Keep it simple and clear!

Hello! I’m trying to make a player go in pvp mode when touching a platform, i used a for loop to detect when a part in the platform gets touched.

  1. What is the issue? Include screenshots / videos if possible!

The bool value is not changing nor printing anything

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried changing pairs to ipairs, and added a while wait() do, i also looked at dev hub and devforum but i cant see anything related to my problem

script:

for _,v in pairs(PM:GetChildren()) do
	print("HEEE")
	v.Touched:connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr.PVP.Value == false then
				plr.PVP.Value = true
			end
		end
	end)
	v.TouchEnded:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr.PVP.Value == true then
				plr.PVP.Value = false
			end
		end
	end)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I’m still learning myself and should probably tread lightly giving advice, but here’ goes: Your for loop is running all of the code below it multiple times for all children inside of “PM.” Is “PM” a folder that includes multiple parts that would make the player’s PVP value true? If you have only a single PVP area, I would say you don’t need the touched events inside of a loop, instead, you can run one on the PVP platform that makes PVP.Value true, and another on whatever surface(s) are touched when the player moves outside of the PVP platform that makes the value false. I tend to brute force things because I don’t always know best practice, but I’m envisioning a square part that is the PVP platform, bordered by 4 transparent, no collide parts that would be the ‘non-pvp’ triggers. You would need a debounce in your touched functions also.

Yes, it is a folder with multiple parts

What do you think is a better way to do it? Do I brute force it or just make one big square that captures the platform, and make it the one single pvp area?

edit: or just put a script on every individual part

probably asking basic questions, but is your script even printing Heee? If not, is ‘PM’ a variable for the Terrain folder? You should be seeing Heee for each rockfloor1.

Also the first “connect” in your script needs to be capital C. Maybe that’s it.

Nope, its not printing, ‘PM’ isn’t a variable. I also noticed that capitalization and yes I changed it but it didnt work

Ahh. Assuming Terrain is a folder in your workspace then the first line needs to be:

for _,v in pairs(workspace.Terrain:GetChildren()) do

Yeah, not working :frowning:

( [ { more characters } ] )

Where exactly is the Terrain folder? Could you show your explorer?

We won’t be able to help you with just the given information, we need the information of the variable PM, this could be a nil or different folder, we also need the screen shot of the workspace to identify the location of the assigned variable from the PM.

1 Like

Workspace:
image
Full script:

local PM = game.Workspace.Terrain

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local PVP = Instance.new("BoolValue")
		PVP.Name = "PVP"
		PVP.Value = false
		PVP.Parent = character
	end)
end)

for _,v in pairs(workspace.Terrain:GetChildren()) do
	print("HEEE")
	v.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local character = hit.Parent
			if character.PVP.Value == false then
				character.PVP.Value = true
			end
		end
	end)
	v.TouchEnded:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local character = hit.Parent
			if character.PVP.Value == true then
				character.PVP.Value = false
			end
		end
	end)
end

Don’t name your folder “Terrain” - Your script is seeing the Terrain service, which is for making real terrain in your game. Pick a new name and try that.
image

OH… bruh, my bad, silly of me :sweat_smile:

1 Like