Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a better way to execute this? What's your opinion, how would you do it? (Just for fun)

Asked by
ElBamino 153
3 years ago

Just a question for my scriptinghelpers community (awesome community everybody is so helpful and nice). So, personally I would say this is more of a personal preference but, I do know for a fact there is always a better way to do something. So I'm going to provide a code below that works that I wrote to award a badge on touch. I was wondering if there was a better way to do this, more effective if you will. Maybe not as easy to like break or be exploited (don't think it would be but still a thought I always consider nowadays you never know what people will exploit). Very simply script, basic stuff right here everybody learns at the start I would say. Important but basic. So I was wondering if there is anything I'm maybe missing out on, or you know whatever. I can't wait to hear your feedback! Thank you for the responses in advance, every productive response will receive an upvote and, I'll be selecting the answer to the question with the most help versus the best code. Like I said thanks again, this is 100% just for fun, I'm looking for opinions.

local BadgeId = 2124749339
script.Parent.Touched:Connect(function(hit)
    if hit.Parent == nil then return end
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player == nil then return end
    game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeId)
end)

1 answer

Log in to vote
2
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

it’s completely fine, not much more you can do for like seven lines. if you wanted to get really nitpicky there are two things I would have done differently.

  1. perhaps this is just personal preference but I almost never use GetService on the same line as something else, usually I just put services at the top of my script as variables unless the script is extremely small. for me they’re kind of on the same level as requires, plus they usually have unique names so there should never problems with variable name collision. not sure if others do this.
  2. when you have a constant variable you usually write it in all caps and underscores, it makes it obvious that it’s not supposed to be changed. usually this is for IDs, math, tickspeeds, prewritten messages and other things like that. in this case you would write BadgeId as BADGE_ID. it makes it obvious that it was not calculated somewhere, but specifically set by the developer. you should also put them around the top of the script next to the requires and stuff so you can change them easily, not inside of functions or in the middle of the script.
0
That makes a lot of sense, thank you for the advice! I appreciate it. Minor things that make a difference really. ElBamino 153 — 3y
Ad

Answer this question