[Basic Help] Scripting

Basically I want to know what this code do so far

local debounce = false
local groupId = 5016099 -- id of your group


detect.Touched:Connect(function(hit)
	if debounce then return end
	if(game.Players:GetPlayerFromCharacter(hit.Parent)) then --Part that hit the part is indeed a player
		debounce = true
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		print(player.Name)
		if (player:IsInGroup(groupId)) then
			print("in the group")
			
		
		end
		wait(5)
		debounce = false
	end
end)```

I want to know what those actually do so I can understand and help me to write this code later when needed.
1 Like

So first, you have to format your code correctly, this can be done using three backticks or three squigglies:

Input:
```
-- code here
```

This works too:
~~~
-- code here
~~~

Output:

-- code here

Secondly, that looks like a VIP door script that checks if a player is in a group (yours), and prints in the group if they are. It also comes with a debounce, in the form of a debounce variable, so the door script waits 5 seconds before checking for a player again.

I know that part, but the idea is know what each command means to them do there.

1 Like

Ok, let’s do that then:

This connects a function to detect's .Touched event, source here:

detect.Touched:Connect(function(hit)

This stops the function early if debounce == true:

if debounce then return end

This checks if the part hit is part of a player’s character, as you can tell by the comment. The function also returns a player if one belongs to this character, source here:

if(game.Players:GetPlayerFromCharacter(hit.Parent)) then --Part that hit the part is indeed a player

This checks if the player is in a group, source here:

if ( player:IsInGroup(groupId) ) then

This reverts debounce to false after 5 seconds:

wait(5)
debounce = false

If you have any more questions about what each function does, make sure to consult the dev wiki (Documentation - Roblox Creator Hub).

1 Like

Alright I’m really thankful for your help there.

1 Like

(Completely Optional) You may want to use :GetGroupsAsync() and checking if the user is in the group this way rather than :IsInGroup() because it won’t cache.

It doesn’t cache, though it yields. GetGroupsAsync takes significantly longer to run than IsInGroup because it gets a list of all your groups from the site instead of checking your presence in one group.

From my personal experience, I’ve ended up having to cache results from GetGroupsAsync anyway to make it effective. So long as you’ve got a good cache system running for it, then it’s fine to use.

For me at least, it doesn’t take a noticeable amount of time longer. I am a developer for a customer service group, which is why I use :GetGroupsAsync() because we wouldn’t be able to afford not having user’s roles update once their ranks are changed. If there are less mass rank changes, then yes :IsInGroup might be the better way to go.

Rank changes don’t mean anything. IsInGroup checks your presence in a single group, GetGroupsAsync gets standings across all your groups. If you call GetGroupsAsync multiple times in a row, you will notice a difference.

A call from IsInGroup takes 0.104 seconds on first call and then an average of 3.43x10-5 seconds when the result is cached. GetGroupsAsync takes around 0.21 seconds each call. This isn’t including the iteration you need to perform on the return array or what you do with it.

GetGroupsAsync takes significantly longer if you don’t memoise or cache (don’t permanently cache though, clear it after some seconds in the background in a different script).

1 Like