You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want to make it so my character is teleported from one stage to another. For example, if on leaderstats it says you are on stage 5, it will take you to stage 6.
What is the issue? I could do this in many ways, but the way I want to do it seems too hard. I thought that maybe I could do it, based on finding a spawn (the part your player will get teleported to one they complete a level) based on your leaderstats level. For example, if it saw that you were on floor one in leaderstats, it would take you to floor two because it sees what floor your on the leaderstats.
This is my tower. It is randomly generated. This is the first level I have created. Each floor of the tower is blocked by a roof – so the floors are separated.
When you touch the yellow sparkly thing, it takes you to the next floor.
What solutions have you tried so far? I have a solution, I just simply want to make it easier to do this – so my code isn’t a mess of if statements. Any ideas?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
function nextLevel(character)
local level = script.Parent.Parent.Level
if level.Value == 2 then
character.HumanoidRootPart.Position = workspace.Floor2.Spawn2.Position
end
end
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Level.Value == script.Parent.Parent.Level.Value - 1 then
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
script.Parent.correct:Play()
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
end
nextLevel(hit.Parent)
end
end)
I want to make it so the first function is simplified. I have started by making it for the first level (the teleporter). I don’t want to create an if statement for each one of these teleporters. And the teleporters will be put into each piece. A piece (a map) will have one teleporters each. I will also make one teleporter for the last part – but I will deal with that when the time comes, I don’t need help with that now.
Also, heads up I am new to the devforum and ROBLOX – only a year of experience.
Thanks for all the help and feedback; I really appreciate it!
just make it find the level of the floor from the level value.
function nextLevel(character)
local level = script.Parent.Parent.Level
character.HumanoidRootPart.Position = workspace["Floor"..level.Value]:FindFirstChildOFClass("SpawnLocation").Position
end
This is assuming the floor is what you mean by level.
function nextLevel(character)
local level = script.Parent.Parent.Level
character.HumanoidRootPart.Position = workspace[Floor .. level.Value].Spawn2.Position
end
Also, you might want to change the humanoidrootpart’s CFrame instead of the Position, since that would teleport only the humanoidrootpart, and leave the rest of the body behind.
(I’m assuming every floor has a spawn named “Spawn2”)
Sorry, I didn’t mean that. Each tower level has a spawn of a different name. Level 1 doesn’t have one. Level 2 has Spawn 2. Level 3 has Spawn 3. Level 4 has Spawn 4, and so on, and so on.
function nextLevel(character)
local level = script.Parent.Parent.Level
character.HumanoidRootPart.Position = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end
This should work. Let me know if it doesn’t, along with any errors in the output. There is probably some stupid beginner spelling mistake I overlooked
It would also be helpful if you provided a screenshot of your workspace, so we have a better idea of what the parenting looks like. (Wow, that sentence sounds weird. “What the parenting looks like” XD)
Something else: I saw that you were using a heck ton of spawn locations, so for future reference, you can actually use just one large spawn location, if you want players to spawn in different locations. (Assuming that’s what you want)
function nextLevel(character)
local level = script.Parent.Parent.Level
character.HumanoidRootPart = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position
end
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Level.Value == script.Parent.Parent.Level.Value - 1 then
player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
script.Parent.correct:Play()
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
end
nextLevel(hit.Parent)
end
end)
No, no, its fine. Just fix the mistake
Also, I recommend you delete your posts where you give a download link to your game file, as someone can easily steal your game.
Position is not spelled Positon
I recommend checking all your words on google to make sure they’re spelled correctly, as that can save a lot of time. Once I wasted several days of my like because I spelled Motor as Moter XD.
function nextLevel(character,curlevel)
local level = tonumber(curlevel)
level = level + 1
local countoflevels = # -- Write count of the levels
if level <= countoflevels then
local count = level
level = "Floor" .. tostring(level) ​
character.HumanoidRootPart.Position = workspace:FindFirstChild(level):FindFirstChild(tostring("Spawn") .. count).Position
game.Players:GetPlayerFromCharacter(character).leaderstats.Level.Value = count
end
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = hit.Parent
nextLevel(plr,game.Players:GetPlayerFromCharacter(plr).leaderstats.Level.Value)
end
end)
If you find any errors, please tell me and it will be fixed.
To you to understand, I am making string and number operations here, so It will automatically be spawned to the level+1’s position, tell me if it is fixed.
Thank you! I try to put .Value in the thing because it never works when I reference in variables. I have figured out the code now, but thank you both of you!