Part toggle chat command working incorrectly

Hey! So, I have a chat command (!togglewalls), which should toggle the nearest tryout pad (union part), if the player is close enough to one of them, and if they have certain ranks in a specific group.

It mostly seems to work, the tryouts pads do toggle on and off, if I’m close enough. However, there is one small issue. It always toggles the tryout pad #2 for some reason. It might be a problem with the checkPermissionAbility() function, though, I’m not sure how to fix it.

ServerScript

local TryoutPads = game.Workspace:WaitForChild("TryoutPads")

local GroupIDs = {14797041, 15328639, 15326631, 15326530, 15328073, 15328147, 15328109, 15328173}
local GroupRanks = {248, 5, 4, 6, 4, 5, 5, 2}

local function checkPermissionAbility(Player)
	if Player:IsInGroup(GroupIDs[1]) and Player:GetRankInGroup(GroupIDs[1]) >= GroupRanks[1] then
		return true
	elseif Player:IsInGroup(GroupIDs[2]) and Player:GetRankInGroup(GroupIDs[2]) >= GroupRanks[2] then
		return true
	elseif Player:IsInGroup(GroupIDs[3]) and Player:GetRankInGroup(GroupIDs[3]) >= GroupRanks[3] then
		return true
	elseif Player:IsInGroup(GroupIDs[4]) and Player:GetRankInGroup(GroupIDs[4]) >= GroupRanks[4] then
		return true
	elseif Player:IsInGroup(GroupIDs[5]) and Player:GetRankInGroup(GroupIDs[5]) >= GroupRanks[5] then
		return true
	elseif Player:IsInGroup(GroupIDs[6]) and Player:GetRankInGroup(GroupIDs[6]) >= GroupRanks[6] then
		return true
	elseif Player:IsInGroup(GroupIDs[7]) and Player:GetRankInGroup(GroupIDs[7]) >= GroupRanks[7] then
		return true
	elseif Player:IsInGroup(GroupIDs[8]) and Player:GetRankInGroup(GroupIDs[8]) >= GroupRanks[8] then
		return true
	else
		return false
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.Changed:Connect(function(Prop)
		if Prop == "Character" then
			repeat wait(.1) until Player.Character
			local Character = Player.Character
			if checkPermissionAbility(Player) == true then
				Player.Chatted:Connect(function(Message)
					local Words = string.split(Message, " ")
					if Words[1] == "!togglewalls" then
						local Closest
						local PlayerPosition = Character.PrimaryPart.Position
						if unpack(TryoutPads:GetChildren()) ~= nil then
							for i,v in pairs(TryoutPads:GetChildren()) do
								if Closest == nil then
									Closest = v
								else
									if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
										Closest = v
									end
								end
							end
						end
						if Closest ~= nil then
							for i,v in pairs(TryoutPads:GetChildren()) do
								if v == Closest then
									if (PlayerPosition - v.Position).Magnitude <= 50 then
										if v.Transparency == 1 then
											for i = 1,0,-0.1 do
												wait(0.02)
												v.Transparency = i
											end
											v.Transparency = 0
											v.CanCollide = true
											v.CanTouch = true
										elseif v.Transparency == 0 then
											for i = 0,1,0.1 do
												wait(0.02)
												v.Transparency = i
											end
											v.Transparency = 1
											v.CanCollide = false
											v.CanTouch = false
										end
									end
								end
							end
						end
					end
				end)
			elseif checkPermissionAbility(Player) == false then 
				print("You are not allowed to use this command!")
			end
		end
	end)
end)

obrazek

It looks like the issue might be with how you’re determining the closest tryout pad.

This code makes use of math.huge to initialize ClosestDistance and simplifies the process of finding the closest pad. I’ve also removed unnecessary checks for the existence of pads and made it check for permission only when the chat command is invoked.

Let me know if this works.

[removed broken code, going to rewrite]

1 Like

Hey! Thanks for your reply, however, it still always toggles the tryout pad #2 for some reason. (and it’s always just that, not a different one)

After a closer look, it may be because you’re iterating through every pad, even if you pass the closest one.

Give this a try?

local TryoutPads = game.Workspace:WaitForChild("TryoutPads")

local GroupIDs = {14797041, 15328639, 15326631, 15326530, 15328073, 15328147, 15328109, 15328173}
local GroupRanks = {248, 5, 4, 6, 4, 5, 5, 2}

local function checkPermissionAbility(Player)
    for i = 1, #GroupIDs do
        if Player:IsInGroup(GroupIDs[i]) and Player:GetRankInGroup(GroupIDs[i]) >= GroupRanks[i] then
            return true
        end
    end
    return false
end

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local PlayerPosition
        Character:WaitForChild("HumanoidRootPart").Changed:Connect(function()
            PlayerPosition = Character.PrimaryPart.Position
        end)
    end)

    Player.Chatted:Connect(function(Message)
        if Message == "!togglewalls" then
            if not checkPermissionAbility(Player) then
                print("You are not allowed to use this command!")
                return
            end
            
            local Closest
            local PlayerPosition = Player.Character and Player.Character.PrimaryPart and Player.Character.PrimaryPart.Position
            if not PlayerPosition then return end

            local ClosestDistance = math.huge
            for _, Pad in pairs(TryoutPads:GetChildren()) do
                local Distance = (PlayerPosition - Pad.Position).magnitude
                if Distance < ClosestDistance then
                    Closest = Pad
                    ClosestDistance = Distance
                end
            end

            if Closest and ClosestDistance <= 50 then
                if Closest.Transparency == 1 then
                    for Transparency = 1, 0, -0.1 do
                        wait(0.02)
                        Closest.Transparency = Transparency
                    end
                    Closest.Transparency = 0
                    Closest.CanCollide = true
                    Closest.CanTouch = true
                elseif Closest.Transparency == 0 then
                    for Transparency = 0, 1, 0.1 do
                        wait(0.02)
                        Closest.Transparency = Transparency
                    end
                    Closest.Transparency = 1
                    Closest.CanCollide = false
                    Closest.CanTouch = false
                end
            end
        end
    end)
end)

1 Like

Yep, works now! Thank you so much.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.