Coding for the first time! [Scripts Included]

Hey, devs! It’s a-me, Harmony! So finally, I’m learning to script in Roblox and these are some things that I coded for the first time by myself.

robloxapp-20210902-1336586

First part changes color and transparency when the player touches the part.

Second part is a Proximity Prompt which again changes color from black to white.

Third & fourth parts are kill and heal parts. (Red part damage’s the player and the Green part Heals the player to full health.

Scripts:

-- Script of the 1st part:

local part = game.Workspace.part

part.Touched:Connect(function()
	
	while wait(1) do
		part.BrickColor = BrickColor.Black()
		wait(1)
		part.Transparency = 0.60
		wait(1)
		part.Transparency = 0
		wait(1)
		part.BrickColor = BrickColor.White()
	end
	
end)
-- Script of the 2nd part: 

local invpart = game.Workspace.invpart
local invpart = script.Parent

invpart.ProximityPrompt.Triggered:Connect(function(player)
	invpart.Transparency = 0
	invpart.BrickColor = BrickColor.Black()
end)
-- Script of the 3rd & 4th parts:
-- KILL PART: 

local killpart = game.Workspace.killpart
local killpart = script.Parent

local function PlayerTouched(killpart)
	local Parent = killpart.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = 20
	end
end

killpart.Touched:Connect(PlayerTouched)

---------------------------------------------------------------------

--HEAL PART:

local healthpart = game.Workspace.healthpart
local healthpart = script.Parent

local function PlayerTouched(healthpart)
	local Parent = healthpart.Parent
	game.Players:GetPlayerFromCharacter(Parent)
	Parent.Humanoid.Health = 100
end

healthpart.Touched:Connect(PlayerTouched)

Feedback and suggestions would be appreciated!!

:memo: - Harmony

9 Likes

Such a proud feeling isn’t it?
I’m still working on making my own scripts so I don’t know if i should give out any feedback, all I can say is good job and good luck with your scripting journey!

So far, these are the best lines of code I’ve seen from a beginner. Particularly in the case that I haven’t seen any Plr variables and there’s good indentation and syntax. With a bit more experience using the Roblox API you’ll be a good programmer in no time.

An improvement I’ve noticed you could make is instead of indexing the part directly from the workspace you should use FindFirstChild if the script is running on the server, or WaitForChild if the script is not running on the server.

There’s also no real reason to use game.Workspace. There are several ways you can get the workspace.

game.Workspace
game:GetService("Workspace")
workspace -- recommended

Example:

-- Script | Serverside Modulescript
workspace:FindFirstChild("killpart")
-- Localscript | Locally ran Modulescript
workspace:WaitForChild("killpart")

Inside of the function PlayerTouched(healthpart) your parameter has the same name as your variable healthpart. The actual parameter for that function would be as follows. healthpart.Touched:Connect(function(otherPart)) where PlayerTouched(healthpart) is actually PlayerTouched(otherPart). I’d recommend using a different variable name in this case instead of a name that’s already in use by a variable.

Why are you using game.Players:GetPlayerFromCharacter in the Heal Part? You don’t set the results to any variable so it doesn’t do anything here. You can check if the part is from a humanoid by doing otherPart.Parent:FindFirstChild("Humanoid")

With the first KillPart script your use of game.Players:GetPlayerFromCharacter would make this kill part only work on players and not NPCs. This is useful if you don’t want the part to affect NPCs.

Those are the only things I wanted to point out. Very good job, and I look forward to your future creations.

1 Like

just wanted to say that the kill part and heal part variables are bad practice

the top two variables are the same thing and have the same name, and the first Touched parameter has the same variable name

also the heal part will error if a Humanoid doesn’t exist under Parent
you did a check on the kill part, but not the heal part

1 Like

Yea, like I’m the first ever person to learn and teach Lua in Roblox. Haha

Yes, I know what you’ve mentioned here. But I could not remember it at that time so I typed game.workspace again and again. :sweat_smile:

1 Like

Yes, actually I know that I need to type otherpart but I thought of typing in my own version to understand better. Thank you for correcting my mistake(s)