For i,v loop problem

Hello, I’ve made a localscript that allows me to “Wall run”, in fact there is just a BodyVelocity to fall slowly.
To detect if the player goes near the wall, i used region3.

local found = false

while wait(1) do
	local WallsFolder = game.Workspace:WaitForChild("Walls")
	for i,v in pairs(WallsFolder:GetChildren()) do
		local player = game.Players.LocalPlayer
		local char = player.Character
		found = false
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
		for _, parts in pairs(parts) do
			if parts:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				--print("player found")
				found = true
				break
			else
				found = false
				--print("player not found")
			end
		end
		if found == true then
			print("found")
			ContextActionService:BindAction("WallJump",function(_,state)
				if state == Enum.UserInputState.Begin then
					print("ContextActionService")
					if v.bounce.Value == true then
						print("found2")
						char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						if char.Humanoid:GetState() ~= Enum.HumanoidStateType.Running then
							if char.HumanoidRootPart.BodyVelocity.MaxForce == Vector3.new(0,0,0) then
								char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,1000000,0)
							elseif char.HumanoidRootPart.BodyVelocity.MaxForce == Vector3.new(0,1000000,0) then
								char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
								v.bounce.Value = false
							end
						end
					end
					char.Humanoid.StateChanged:Connect(function(oldState, newState)
						if newState == Enum.HumanoidStateType.Landed and found == true then
							WallRun:FireServer(Vector3.new(0,0,0))
							char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
							v.bounce.Value = true
						end
					end)
				end
			end, true, Enum.KeyCode.Space)
		else
			ContextActionService:UnbindAction("WallJump")
			char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
			v.bounce.Value = true
		end
	end
end

This script should detect player detection for each wall, but i only does for one wall
robloxapp-20200714-0027534.wmv (2.0 MB)
As you can see here.

Had the same issue with the water system I was making. Here is the fix:

local found = false

while wait(1) do
	local player = game.Players.LocalPlayer
	local char = player.Character
    local foundCheck = false
	local WallsFolder = game.Workspace:WaitForChild("Walls")
	local wall
	for i,v in pairs(WallsFolder:GetChildren()) do
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
		for _, parts in pairs(parts) do
			if parts:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				--print("player found")
				foundCheck = true
				wall = v
				break
			end
		end
    end

        found = foundCheck

		if found == true and wall ~= nil then
			print("found")
			ContextActionService:BindAction("WallJump",function(_,state)
				if state == Enum.UserInputState.Begin then
					print("ContextActionService")
					if wall.bounce.Value == true then
						print("found2")
						char.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						if char.Humanoid:GetState() ~= Enum.HumanoidStateType.Running then
							if char.HumanoidRootPart.BodyVelocity.MaxForce == Vector3.new(0,0,0) then
								char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,1000000,0)
							elseif char.HumanoidRootPart.BodyVelocity.MaxForce == Vector3.new(0,1000000,0) then
								char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
								wall.bounce.Value = false
							end
						end
					end
					char.Humanoid.StateChanged:Connect(function(oldState, newState)
						if newState == Enum.HumanoidStateType.Landed and found == true then
							WallRun:FireServer(Vector3.new(0,0,0))
							char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
							wall.bounce.Value = true
						end
					end)
				end
			end, true, Enum.KeyCode.Space)
		else
			ContextActionService:UnbindAction("WallJump")
			char.HumanoidRootPart.BodyVelocity.MaxForce = Vector3.new(0,0,0)
			wall.bounce.Value = true
		end
end

I think the bounce BoolValue is not defined or not found fast enough, in the wall,because i got an error.Here it is. I tried to put a waitforchild to wait the value to be defined but it doesnt works. This error only happens here in the script, i think its because this part of the script is executed at the first 0.1 second when i hit play. So what can i do ?
error

Maybe try changing this:

if wall.bounce.Value == true then

To this?

if wall:FindFirstChild("bounce") and wall.bounce.Value == true then

Also is the bounce boolValue even in the wall when the script starts running?

Yes i created the boolValue in the wall before i hit play. But changing this line doesnt change anything to the problem.

Just realised: the error actually means that the script is trying to find “bounce” in a object that doesnt exist.

Im kinda stumped, dont know how to solve this

EDIT: Maybe try this? No idea

if parts:FindFirstAncestor(game.Players.LocalPlayer.Name) then
	--print("player found")
	foundCheck = true
	wall = WallsFolder:FindFirstChild(v.Name)
    print("in for loop: "..wall.Name) -- check if it prints anything out
	break
end

And then later

found = foundCheck
print("outside for loop: "..wall.Name) -- check if it prints something again

Now the error is that.
error

now im super confused, how can something that was set a line earlier suddenly nil

i tried all the day and i still cant solve that problem

i finally made it, i just changed the bounce value in each wall into the script, then each player has his bounce value, its working ! Thanks a lot for your help.

1 Like