Code not working correctly

  1. What do you want to achieve? I want to make it so I have chunks of obbies that randomly spawn in each tower, and a teleporter that teleports you to the next level – using position, it is not teleporting you to different experiences.

  2. What is the issue? @MintSoapBar helped me with my code. Look here for more details on my journey: How to make code simpler.

When I step on the teleporter the script is not working correctly. It loops through and looks at every model in this folder, however, when I spawn the chunk at the beginning, the code doesn’t note that a new obby part has been made.

worskpace

My workspace

2

Me climbing to get to the top of the tower and get to the teleporter.

-- local ObbyParts = game.Workspace.ObbyParts
local Sound = script.correct

function nextLevel(character)
	local level = game.Players:GetPlayerFromCharacter(character).leaderstats.Level
	character.HumanoidRootPart.Position = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end

for Index, ObbyPartsChild in pairs(ObbyParts:GetChildren()) do
	for Index, Part in pairs(ObbyPartsChild:GetChildren()) do
		if Part.Name == "Teleporter" then
			Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player.leaderstats.Level.Value == Part.Parent.Level.Value - 1 then
						player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
						Sound:Play()
						player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
					end
					nextLevel(hit.Parent)
				end
			end)
		end
	end
end

Any help would be greatly appreciated! Have a good day!

1 Like

thats because code ran faster than the chunk loading and if you are adding part though a folder and you want the sccript to note that then you should use the .ChildAdded event here is an example

local ObbyParts = ---ur obby parts
local Sound = script.correct

function nextLevel(character)
	local level = game.Players:GetPlayerFromCharacter(character).leaderstats.Level
	character.HumanoidRootPart.Position = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end
ObbyParts.ChildAdded:Connect(function(Part) ---noting everytime something got inserted in the folder
   if Part.Name == "Teleporter" then
			Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player.leaderstats.Level.Value == Part.Parent.Level.Value - 1 then
						player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
						Sound:Play()
						player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
					end
					nextLevel(hit.Parent)
				end
			end)
end)
1 Like

Based on what I understand, in your script you only check for parts once, but you should check every time a new part is added. You iterate through the script before a teleporter part is created, so the script basically iterated through nothing and just stopped there.

As @MrchipsMa ust said, use childadded.

Now getting mad at me :face_with_thermometer:

oh i forgot to add another end here it is

ObbyParts.ChildAdded:Connect(function(Part) ---noting everytime something got inserted in the folder
   if Part.Name == "Teleporter" then
			Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player.leaderstats.Level.Value == Part.Parent.Level.Value - 1 then
						player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
						Sound:Play()
						player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
					end
					nextLevel(hit.Parent)
				end
			end)
          end ------this end i forgot to add
end)
1 Like

Thanks so much! I’ve figured it out. I’m testing right now.

worskpace

On a side note, I see you used free models. Be sure to check any and all free models to viruses as they can ruin your game and also get your account banned.

NOPE

Nothing happening when I stand on it ):

Is there anything relevant on the output? Can you show me a screenshot?
Also try debugging by putting prints before if statements so you can see where the script stops and fix the if statement.

This is the whole screen

wait(5)
workspace.BuildEvent:Fire()
print("Fired!")

This is my code to fire the building of a chunk.

workspace.BuildEvent.Event:Connect(function()
	local folder = game.ServerStorage.Parts:GetChildren()
	local chosenOne = folder[math.random(1, #folder)]
	local part = chosenOne:Clone()
	part.Parent = workspace.ObbyParts
	part:SetPrimaryPartCFrame(script.Parent.CFrame)
end)

This is my code to build a chunk. And you know the rest.

This is the output

So nothing relevant on output.

Also try debugging by putting prints before if statements so you can see where the script stops and fix the if statement.

Try this.

1 Like

This was my results. Still not working. I couldn’t share it very well, sorry. First time putting a video into ROBLOX.

As I said, try debugging by putting prints before if statements so you can see where the script stops and fix the if statement.

ObbyParts.ChildAdded:Connect(function(Part) ---noting everytime something got inserted in the folder
   if Part.Name == "Teleporter" then
            print("Part is a teleporter")
			Part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") then
print("Humanoid was found!")
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if player.leaderstats.Level.Value == Part.Parent.Level.Value - 1 then
print("Player level is less than part level").
						player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
						Sound:Play()
						player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
					end
					nextLevel(hit.Parent)
				end
			end)
          end ------this end i forgot to add
end)

Like this.

Then check output to see if any of those prints printed, and show me the output.

Hello EarthyViking! So, how my code works, is it goes into all the ObbyParts Children, and gets their children, and looks for a part named “teleporter”. However, since I see you have made some changes to the game, it won’t work that way anymore.

local Sound = script.correct

function nextLevel(character)
	local level = game.Players:GetPlayerFromCharacter(character).leaderstats.Level
	character.HumanoidRootPart.Position = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end

for Index, Floor in pairs(workspace:GetChildren()) do
	if Floor.ClassName == "Model" then
		for Index, Part in pairs(Floor:GetChildren()) do
			if Part.Name == "Teleporter" then
				Part.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						if player.leaderstats.Level.Value == Part.Parent.Level.Value - 1 then
							player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
							Sound:Play()
							player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
							end
						nextLevel(hit.Parent)
					end
				end)
			end
		end
	end
end

This might not work as intended, as I don’t know exactly what your workspace looks like, so you might have to give me another file link to your game. Remember to send it to me in private messages, and not in this thread, as game-stealers exist :wink:

1 Like

Alright, just replace the teleporter script with this:

local ObbyParts = game.Workspace.ObbyParts
local Sound = script.correct

function nextLevel(character)
	local level = game.Players:GetPlayerFromCharacter(character).leaderstats.Level
	character.HumanoidRootPart.Position = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end

ObbyParts.ChildAdded:Connect(function(Child)
	local Teleporter = Child:WaitForChild("Teleporter")
	Teleporter.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player.leaderstats.Level.Value == Teleporter.Parent.Level.Value - 1 then
				player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
				Sound:Play()
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
			end
			nextLevel(hit.Parent)
		end
	end)
end)

Lemme test it real quick. I will notify you when it works.

Thank you @MintSoapBar! You’re the best! I got it to work (: