How to make code simpler

You can write your topic however you want, but you need to answer these questions:

  1. 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.

  2. 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.

2

When you touch the yellow sparkly thing, it takes you to the next floor.

  1. 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 :sweat: I am new to the devforum and ROBLOX – only a year of experience.

Thanks for all the help and feedback; I really appreciate it!

4 Likes

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.

I think you can try

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.

Ah, ok, I’ll change the script a little then.

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 :sweat_smile:
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)

Okay I will send that! Thanks for letting me know! Shall I send the file?

Let me test it out and see if it works first.

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)

Oh, okay! Thanks for the info! I will note that.

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)

you appear to have forgotten the “.Position” in character.HumanoidRootPart = workspace…

1 Like

Oh god, sorry, that was a bad mistake on my part.

No, no, its fine. Just fix the mistake :wink:
Also, I recommend you delete your posts where you give a download link to your game file, as someone can easily steal your game.

Probably just another dumb mistake on my part, but. . . .

character.HumanoidRootPart.Positon = workspace["Floor" .. level.Value]["Spawn" .. level.Value].Position

Position is not spelled Positon :wink:
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.

Okay, thanks so much! You’ve been a great help.

Hmm. Try it like that:

    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.

1 Like

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.

Also, you might want to make your code simpler by adding in more variables instead of typing in long code, e.g.:

script.Parent.Parent.Level.Value

could be

local Level = script.Parent.Parent.Level
Level.Value -- to get its value

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!