Is this Powerup code good for a beginner?

Hello! :smiley:

So this code is intended to create a part which is used as a Powerup for players which will increase their walkspeed and max health by a big amount and it’s only a one time use and I made it so the Powerup can also respawn and I created parts around the Powerup using the code to decorate it I also tried to make it look as good as possible by changing the material, etc…

If you want to test this code in ROBLOX Studio then it’s recommended that you test it in the Baseplate world because that’s where I made this code but you can also just review the code by looking at it and giving me feedback on how to make it better and also if there’s anything to improve on in this code.

Also please tell me if this code is good for a beginner or not and don’t forget to tell me why, this is the first time I create a code like this and also it’s the first time I create a code this big too so I wanted to know what people think, this was very fun to make for me.

Overview:

  • This code will create parts including the Powerup part it’s recommended that if you decide to test this code then test it in the Baseplate world because that is where I made all of this code.
  • I have tried to improve this code as much as I can but I am new to scripting so I am not sure if this code is perfect.
  • I would like to know if there’s anything that I can improve on in this code and if I can make my code look more clean and easy to read by making this code as short as possible.
Code
local part2 = Instance.new("Part")
part2.Anchored = true
part2.BrickColor = BrickColor.new("Sand green")
part2.Position = Vector3.new(-38,0.5,7.5)
part2.Size = Vector3.new(6,1,1)
part2.Material = Enum.Material.Metal

part2.Parent = game.Workspace

local part3 = Instance.new("Part")
part3.Anchored = true
part3.BrickColor = BrickColor.new("Sand green")
part3.Position = Vector3.new(-34.5,0.5,10.5)
part3.Size = Vector3.new(1,1,5)
part3.Material = Enum.Material.Metal

part3.Parent = game.Workspace

local part4 = Instance.new("Part")
part4.Anchored = true
part4.BrickColor = BrickColor.new("Sand green")
part4.Position = Vector3.new(-38,0.5,13.5)
part4.Size = Vector3.new(6,1,1)
part4.Material = Enum.Material.Metal

part4.Parent = game.Workspace

local part5 = Instance.new("Part")
part5.Anchored = true
part5.BrickColor = BrickColor.new("Sand green")
part5.Position = Vector3.new(-41.5,0.5,10.5)
part5.Size = Vector3.new(1,1,5)
part5.Material = Enum.Material.Metal

part5.Parent = game.Workspace

local part6 = Instance.new("Part")
part6.Anchored = true
part6.BrickColor = BrickColor.new("Sand green")
part6.Position = Vector3.new(-38,1,10.5)
part6.Size = Vector3.new(6,2,5)
part6.Material = Enum.Material.Metal

part6.Parent = game.Workspace

function createpart(anchored,transparency,brickcolor)
	local part1 = Instance.new("Part")
	part1.Name = "Powerup"
	part1.Anchored = anchored
	part1.Transparency = transparency
	part1.BrickColor = brickcolor
	part1.Position = Vector3.new(-38,3,10.5)
	part1.Parent = game.Workspace
	part1.Size = Vector3.new(4,2,3)
	part1.Material = Enum.Material.Glass
	
	return part1
end

local returnedpart1 = createpart(true,0,BrickColor.new("Deep orange"))

 returnedpart1.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
			hit.Parent.Humanoid.WalkSpeed = 30
			hit.Parent.Humanoid.MaxHealth = 200
			wait(0.2)
			hit.Parent.Humanoid.Health = 200
			wait(0.3)
			returnedpart1:Destroy()
			print("Powerup used.")
	end
end)

returnedpart1.TouchEnded:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			wait(5)
			hit.Parent.Humanoid.WalkSpeed = 16
			hit.Parent.Humanoid.MaxHealth = 100
			wait(0.2)
			hit.Parent.Humanoid.Health = 100
			wait(0.1)
			print("You lost your Powerup and it cannot be used again.")
			wait(10)
			local powerupclone = returnedpart1:Clone()
			powerupclone.Parent = game.Workspace
			wait(0.5)
			print("Powerup respawned.")
	    end
end)

The truth of the matter is, in cases like this where the part’s properties are pre-determined and the parts will be there from the start, it’s better to just create them in editor directly instead of using a script.

Alright, I just wanted to try and create them inside the script.

Yes, you can create parts which the script takes and copies without having to set stuff in the script

1 Like

Hey @HilyrHere

Do you or anyone who is reading this know if there’s a way to create a loop in which the Powerup will constantly be respawned and it will give the player the same effect (Powerup) when they touch it?

I have tried using a while loop for this but it doesn’t seem to work, is it possible?

Hmm it’s strange that while loops are not working for you, I would have done that, what is the problem?

I just can’t seem to do it ROBLOX Studio will always give me a “Game script timeout” error in the Output

Can you show me how you would do it? Maybe your method will work, maybe i’m doing it wrong.

Oh, if th ere is a game script timeout then it’s probably doing something too fast, in other words you didn’t put a wait() somewhere so it would run way too fast like every time it can

Okay a wait() fixed the problem but it still won’t let me use the powerup again, because when the powerup is used and destroyed it basically can’t find it again even if it respawns so it tells me that Powerup isn’t a valid member of workspace and I think that’s what’s breaking the script I am trying to use this line of code to do it, is it correct?

while game.workspace.Powerup.Touched do

Well I dont think you can use while loops for events like that.

Maybe save the powerup as a variable somewhere or save it in server storage

If I remove the if statements in the code and replace them with while loops in the code I can have the effects forever but now I can’t lose it because the while loops keeps giving me the effects over and over again so the respawning aspect of the Powerup is useless now, I believe that I can’t use while loops for this, do you have any other solutions than while loops? They just honestly won’t work.

Maybe I need to somehow break out of the while loop by using some kind of in-built function or something?

So apparently I figured out that I need to use script.Disabled

Where am I supposed to put my script inside that function exactly? Sorry I am new to scripting.

THANK YOU! The script works perfectly fine and I can get the effects again after using it once!

2 Likes