Is this good code?

Hi, i just wanted to know if this code is good it works but i am just asking if it is clean?

local tool = script.Parent
local handle = tool.Handle

local plr = script:FindFirstAncestorWhichIsA("Player") or game:GetService"Players":GetPlayerFromCharacter(tool.Parent)

local anim:Animation = tool:WaitForChild("R15Slash")

local debounce = false
local debouncecooldown = .5

local activated = false

local function Animate()
	local char = tool.Parent
	if char:FindFirstChild("Humanoid") then
		local hum:Humanoid = char:FindFirstChild("Humanoid")
		local animator:Animator
		
		if hum:FindFirstChild("Animator") then
			animator = hum:FindFirstChild("Animator")
		else
			animator = Instance.new("Animator", hum)
		end
		
		local loadedanim = animator:LoadAnimation(anim)
		loadedanim:Play()
	end
end

local function sickledeactivate()
	wait(0.05)
	activated = false
end

local function sickleturbo()
	if debounce == false then
		debounce = true
		
		Animate()
		
		for i,hit in game.Workspace:GetPartsInPart(handle) do
				for i,v in hit:GetTags() do
					if v == "wheat" then

					plr.leaderstats.wheat.Value += 1

				end
			end

		end

			wait(debouncecooldown)
			debounce = false


	end
end

script.Parent.Activated:Connect(sickleturbo)

script.Parent.Deactivated:Connect(sickledeactivate)

thanks

2 Likes
  • issue 1: script.Parent is an indicator you have ‘hanged’ your script unto one of actual Instances; naive architecture which is usually advised for complete newbies, i.e. script kiddies; also prone, or, i should say, a direct invitation for exploiters to break in and inject their code into your game logic, not to mention they will see your precious scripts
  • issue 2: global variables shared between functions; although required for this given snippet, this coding pattern itself is a bad habit and usually only be used when a pure functional paradigm is being in use, which heavily relies on performant code sharing program state globally

aside from everything stated above, it is a common script snippet controlling tool animations
there is not much to analyse or assess here; first of, it is too small, second, it is unclear how it will be inserted into the overall game logic, bc this is what matters to say if the script is good or not

2 Likes

Don’t worry about it looking clean. The user will not care about it because the user will not see it. As long as it works as intended it’s good enough. You are making a game after all.

3 Likes

plr should be Player. char should be Character. Try to avoid abbreviations unless necessary

Instance names should be capitalized according to roblox style guide, which is not really necessary but helps to make your code more universally understandable. So if you want to then you should capitalize Tool, Handle, Player, Animation

constants like debouncecooldown should be indicated with screaming snake case, so it should be DEBOUNCE_COOLDOWN unless it changes

local hum:Humanoid = char:FindFirstChild("Humanoid") should have a type error if your code is in strict mode. If you are using type annotations then it’s a good idea to use --!strict at the top of your script anyways. Findfirstchild just returns an instance, so it should be local Humanoid = char:FindFirstChild("Humanoid") :: Humanoid or you can use FindFirstChildWhichIsA

in your for loop in the sickleturbo function, the iterator variable i is defined twice. You should use _ which indicates an unused variable if youre not going to use i

If leaderstats fails to load in time then plr.leaderstats.wheat.Value += 1 will fail. You should instead define leaderstats at the start with waitforchild

wait(debouncecooldown) should be replaced with task.wait (more accurate scheduler)

you should use either PascalCase, snake_case, or camelCase instead of writing variables like debouncecooldown because it’s hard to see where the new words are. The style is also inconsistent with function names like Animate and sickleturbo in the same code

In your sickleturbo function you should get rid of useless new lines. There are two of them after the line debounce = false. This function also has mismatched indentation in the for loops. In the if statement in the nested for loop you should use the guard clause pattern to reduce nesting. You should also be more descriptive with the variable name v. So it should be

for _, hit in game.Workspace:GetPartsInPart(handle) do
	for _, tag in hit:GetTags() do
		if tag ~= "wheat" then continue end
		plr.leaderstats.wheat.Value += 1
	end
end

I would also use guard clause in the sickleturbo function to reduce unnecessary nesting

local function sickleTurbo()
	if debounce then return end
	debounce = true
	
	-- other code
	
	task.wait(debounceCooldown)
	debounce = false
end

The function name Animate makes it unclear what the function actually does. Generally function names should have verbs in them, so it should be something like playAnimation

local animator:Animator
		
if hum:FindFirstChild("Animator") then
	animator = hum:FindFirstChild("Animator")
else
	animator = Instance.new("Animator", hum)
end

This pattern can be much cleaner with the if then else statement, which would look like

local Animator : Animator = if hum:FindFirstChild("Animator")
	then hum:FindFirstChild("Animator")
	else Instance.new("Animator", hum)

But i think it might be unnecessary in in the first place because humanoid should always have the animator

On the other hand, like how @ivan866_z said, it’s probably bad architecture to put a server script inside every tool. It’s slow, uses a lot of memory, and hard to scale (hard to add more tools). Instead you should probably just make one system that handles all tools, but it depends on your game.

Overall you need more practice to get the formatting down. Making readable code is absolutely necessary not just for working in teams but also for making development easier for yourself in the future. It’s extremely difficult, tedious, and slow to work on large unreadable code. But don’t obsess too much over readability either

1 Like

It is completely fine just use task.wait() instead of wait(). It is alot better performance wise.

90% Of your code should look like this, just regular code not ugly or bad. It’s just fine. The only time it should look decent is when you’re using OOP. And the majority of the time you won’t be creating systems that require OOP.

1 Like

5 cents how indentation can be improved.

local function Animate()
	local char = tool.Parent
	local hum:Humanoid = char:FindFirstChild("Humanoid")
	
	if not hum then return end
	
	local animator:Animator = hum:FindFirstChild("Animator")
	if not animator then
		animator = Instance.new("Animator", hum)
	end
	
	local loadedanim = animator:LoadAnimation(anim)
	loadedanim:Play()
end

local function sickleturbo()
	if debounce then return end
	debounce = true
	
	Animate()
	
	for i,hit in game.Workspace:GetPartsInPart(handle) do
		for i,v in hit:GetTags() do
			if v ~= "wheat" then continue end

			plr.leaderstats.wheat.Value += 1
		end
	end

	wait(debouncecooldown)
	debounce = false
end
for i,v in hit:GetTags() do
        if v == "wheat" then

        plr.leaderstats.wheat.Value += 1
    end
end

can’t you just use

if hit:HasTag("wheat") then
    -- code
end

whoops sorry about the reply, meant to reply to the original post