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.
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.
My workspace
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!
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)
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.
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)
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.
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.
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.
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)
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
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)