How would I improve this code by adding a group rank and group id?
local Service = game:GetService("TweenService")
local Gate = script.Parent.Parent.Door1
local Allowed = {
1614394440,1287679593 -- your userID
}
local Info = TweenInfo.new(8)
script.Parent.ClickDetector.MouseClick:Connect(function(hit)
for i,v in pairs(Allowed) do
if hit.UserId == v then
print("allowed")
script.Parent.CanCollide = false
local Change = {Position = Vector3.new(594.095, 45.715, 81.882)}
local Tween = Service:Create(Gate, Info, Change)
Tween:Play()
end
end
end)
If you don’t have to use the index value of a for loop, you can just put a _,. It is recommended to use ipairs when looping through arrays. I’d also suggest to have your values in an order.
Will return a player, but you reference the player as a hit. For code readability, you can use player instead of hit as it makes more sense.
Here’s how I would write your code:
local Service = game:GetService("TweenService")
local Gate = script.Parent.Parent.Door1
local Allowed = {
1614394440,
1287679593
}
local Info = TweenInfo.new(8)
local Change = {Position = Vector3.new(594.095, 45.715, 81.882)}
local Tween = Service:Create(Gate, Info, Change)
script.Parent.ClickDetector.MouseClick:Connect(function(player)
for _, v in ipairs(Allowed) do
if player.UserId == v then
script.Parent.CanCollide = false
Tween:Play()
end
end
You have the right idea 100%. I do think you should work on formatting your code better. It’s easy for people reading the code, and makes it 100x easier for yourself to read and understand for when you go over it.