At some point it just looks horrible seeing a huge chunk of the code just be variables. Is there a better way to go about it?
local debounce = false
game.Workspace.Sell.Touched:Connect(function(part)
local humoid = part.Parent:FindFirstChild("Humanoid")
if humoid ~= nil then
if not debounce then
debounce = true
local character = humoid.Parent
local player = game.Players:FindFirstChild(humoid.Parent.Name) if player == nil then return end
local leaderstats = player:FindFirstChild("leaderstats") if leaderstats == nil then return end
local Storage = leaderstats:FindFirstChild("Storage") if Storage == nil then return end
local Cash = leaderstats:FindFirstChild("Cash") if Cash == nil then return end
local basic = character:FindFirstChild("Basic") if basic == nil then return end
basic.Sign.SurfaceGui.Frame.TextLabel.Text = tostring(Storage.Value).."/1000"
basic.Sign.SurfaceGui.Frame.Frame.Size = UDim2.new((Storage.Value/1000),0,1,0)
local StorageUnits = player:FindFirstChild("StorageUnits") if not StorageUnits then return end
local AmountToGive = 0
end
end)
You don’t need to keep redundant checks. Those if == nil are going to clog up your code. There’s also some things that you expect to be there that don’t need to guard against like the player and all the leaderstats items.
Also you should learn about guard clauses also called early returns which basically invert the check of an if statement and break out of your function to prevent nesting. A practical example would look like this:
local Players = game:GetService("Players")
local debounce = false
game.Workspace.Sell.Touched:Connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
--These are guard clauses/early returns. Just evaluate the opposite of the if statement and return if it doesn't work!
if not humanoid then return end --You can also do :if humanoid == nil then return end
if debounce then return end
debounce = true
local character = humanoid.Parent
local player = Players:GetPlayerFromCharacter(character) --Use this to get the player instead
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats == nil then return end
local Storage = leaderstats:FindFirstChild("Storage")
local Cash = leaderstats:FindFirstChild("Cash")
local basic = character:FindFirstChild("Basic")
basic.Sign.SurfaceGui.Frame.TextLabel.Text = tostring(Storage.Value).."/1000"
basic.Sign.SurfaceGui.Frame.Frame.Size = UDim2.new((Storage.Value/1000),0,1,0)
local StorageUnits = player:FindFirstChild("StorageUnits") if not StorageUnits then return end
local AmountToGive = 0
end)
I also encourage you to put proper spaces in your code when neccessary.
local debounce = false
game.Workspace.Sell.Touched:Connect(function(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player and not debounce then
debounce = true
local character = player.Character
local humoid = part.Parent:FindFirstChild("Humanoid")
local leaderstats = player:FindFirstChild("leaderstats")
local Storage = leaderstats:FindFirstChild("Storage")
local Cash = leaderstats:FindFirstChild("Cash")
local basic = character:FindFirstChild("Basic")
basic.Sign.SurfaceGui.Frame.TextLabel.Text = tostring(Storage.Value).."/1000"
basic.Sign.SurfaceGui.Frame.Frame.Size = UDim2.new((Storage.Value/1000),0,1,0)
local StorageUnits = player:FindFirstChild("StorageUnits")
local AmountToGive = 0
end
end)
use GetPlayerFromCharacter, it will return nil just like FindFirstChild if it doesn’t find the player.
Instead of making a variable for everything, just do something like:
if character:FindFirstChild(“Basic”) == nil then return end
It cleans things up a bit. also you could do:
if not humoid then return end
if debounce return end
You don’t need to manually clear them. Locals in Lua are automatically garbage collected when they go out of scope, so once your function ends, all those variables are gone. The only reason to manually set them to nil is if the function stays alive for a long time and holds large objects in memory, which isn’t the case here.