Why script detecting player rank from a block value isn't working?

So, I’m trying to make a UI update whenever a brick is touched. So far, it works great.
I tried to add in a group check, where it checks a value in the brick, see if the player’s rank in that value group is a certain value, and spits out two different answers.
Issue is, Regardless of the group value, it always spits out the “LOCATION UNKNOWN” output, which means it thinks I’m always not in the group.
No error message in the console.

local portals = GUIFolder:GetChildren()

for _,v in pairs(portals) do
	if v:IsA("BasePart") and v:findFirstChild("GUIConfig") then
		v.Touched:connect(function(hit)
			if v.GUIConfig:findFirstChild("LocationColor") then locationTextColor = v.GUIConfig:findFirstChild("LocationColor").Value end	
			if v.GUIConfig:findFirstChild("LocationName") then locationText = v.GUIConfig:findFirstChild("LocationName").Value end
			if v.GUIConfig:findFirstChild("GroupLock") then rank = v.GUIConfig:findFirstChild("GroupLock").Value end
			if hit and hit.Parent and Player.Character and CD == 1 and Player:GetRankInGroup(rank) >= 1 and hit.Parent == Player.Character then
				CD = 2
				targetLocationText = locationText
				targetLocationTextColor = locationTextColor
				typewrite(GUI.TopLeft.LocationText, targetLocationText)
				wait(1)
				CD = 1
			else if Player:GetRankInGroup(rank) < 1 then
					CD = 2
					targetLocationText = "LOCATION UNKNOWN"
					targetLocationTextColor = Color3.fromRGB(255,255,255)
					typewrite(GUI.TopLeft.LocationText, targetLocationText)
					wait(1)
					CD = 1
				end
			end
		end)
	end
end```
1 Like

What is the rank variable set to? The function you’re using requires the group ID, not the expected rank.

Player:GetRankInGroup(GroupID) >= rank

The rank is set to the group ID, and the Group ID is taken from the IntValue in the block the player touches.

You may be running into an issue because you are using else incorrectly. Rather than else if, it should be elseif (all one word). The way you have it, it is going to else, and then running a separate if statement. That may not be the issue though, your first if statement is very cluttered, it may be better to check that hit and hit.Parent and player.Character exist, and then within that if statement, place the ifs with the GetRankInGroup.

I tried fixing both things stated there, loop and elseif.

Same thing occurs, it spits out the ‘Location Unknown’ regardless of the group rank.

local portals = GUIFolder:GetChildren()

for _,v in pairs(portals) do
	if v:IsA("BasePart") and v:findFirstChild("GUIConfig") then
		v.Touched:connect(function(hit)
			if v.GUIConfig:findFirstChild("LocationColor") then locationTextColor = v.GUIConfig:findFirstChild("LocationColor").Value end	
			if v.GUIConfig:findFirstChild("LocationName") then locationText = v.GUIConfig:findFirstChild("LocationName").Value end
			if v.GUIConfig:findFirstChild("GroupLock") then rank = v.GUIConfig:findFirstChild("GroupLock").Value end
			if hit and hit.Parent and Player.Character and CD == 1 and hit.Parent == Player.Character then
				if Player:GetRankInGroup(rank) >= 1 then
					CD = 2
					targetLocationText = locationText
					targetLocationTextColor = locationTextColor
					typewrite(GUI.TopLeft.LocationText, targetLocationText)
					wait(1)
					CD = 1
				elseif Player:GetRankInGroup(rank) < 1 then
						CD = 2
						targetLocationText = "LOCATION UNKNOWN"
						targetLocationTextColor = Color3.fromRGB(255,255,255)
						typewrite(GUI.TopLeft.LocationText, targetLocationText)
						wait(1)
						CD = 1

					end
				end
			end)
	end
end```

Are you sure the rank value is being set correctly? If I were you I’d place print statements all over that code to check variables and the path of the program. Alternatively you can use roblox’s debug tools like Watch and the Debugger.

Ah, the print(rank) was put right after the line to set it, and it returned as 0.
So the line

if v.GUIConfig:findFirstChild("GroupLock") then rank = v.GUIConfig:findFirstChild("GroupLock").Value end

Is what is failing

Oh yea, you’re using the deprecated findFirstChild, capitalize the first F and that may fix it. FindFirstChild.

Nope, rank still returns a 0.
What’s weird is the same line of code to set the UI text as seen above works just fine, but it won’t set the rank here.

Ok then, the problem may either be in the value stored in GroupLock, or it could be whatever Player is set to. What is that variable set to?

Ok two things:
One, I tried changing code to:

rank = v.GUIConfig:FindFirstChild("GroupLock").Value

Doing this, the console spits out “Error: Attempt to index nil with ‘Value’”

Two,

local GUIFolder = workspace:WaitForChild("GUIPortals")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local GUI = game.Players.LocalPlayer.PlayerGui:WaitForChild("HeadsUpDisplay")
local TL = GUI.TopLeft

I’m not entirely sure this is right but maybe your writing the group rank wrong because for owner Roblox puts it as 255 and member is 1 I believe. So if I read this right your checking if your character is a member not owner which I’m assuming you are, try this if it doesn’t work then I’m sorry.

For this basic script I’m just checking if the player is in the group at all, so; rank >= 1? Good, show the title.
Issue is it’s not pulling that group number from the block at all.

local players = game:GetService("Players")
local portals = GUIFolder:GetChildren()

local debounce = false

for _, portal in ipairs(portals) do
	if portal:IsA("BasePart") then
		local guiConfig = portal:FindFirstChild("GUIConfig")
		if guiConfig then
			portal.Touched:Connect(function(hit)
				if debounce then
					return
				end
				
				local hitModel = hit:FindFirstAncestorOfClass("Model")
				if hitModel then
					local hitPlayer = players:GetPlayerFromCharacter(hitModel)
					if hitPlayer then
						debounce = true
						locationTextColor = guiConfig.LocationColor.Value
						locationText = guiConfig.LocationName.Value
						rank = guiConfig.GroupLock.Value
						if hitPlayer:GetRankInGroup(rank) > 0 then
							targetLocationTextColor = locationTextColor
							targetLocationText = locationText
							typewrite(GUI.TopLeft.LocationText, targetLocationText)
						else
							targetLocationTextColor = Color3.new(1, 1, 1)
							targetLocationText = "LOCATION UNKNOWN"
							typewrite(GUI.TopLeft.LocationText, targetLocationText)
						end
						task.wait(1)
						debounce = false
					end
				end
			end)
		end
	end
end
1 Like