Jump to content

How to use multidimensional tables?


Bilal135

Recommended Posts

I just found out that there's a way to store multiple values in a table. So, I planned to make a custom punish system with it. Stored three values in the table, a rule id, a description for what it is, and it's punishment. I'd like to know how can I implement these, for example, if i typed /punish [player] [id], it should automatically derive it's description and punishment from the table, and punish the player. (and output description in the chat)

rules = {
    {["ruleID"] = "1", ["description"] = "Trash talking", ["punishment"] = "kick"}
}

Thanks.

Edited by Lynch
Link to comment
  • Moderators
-- SERVER SIDE

rules = {
    [1] = {description = "Trash talking", punishment = "kick"},
    [2] = {description = "Lot of trash talking", punishment = "ban"},
}

addCommandHandler("punish", function(player, cmd, ruleID, targetPlayerName)
	local ruleID = tonumber(ruleID)
	
    if rules[ruleID] then -- valid rule id
		local targetPlayerElement = getPlayerFromName(targetPlayerName)
		
		if targetPlayerElement then -- target found
			local description = rules[ruleID].description
			local punishment = rules[ruleID].punishment
			local adminName = getPlayerName(player)
			
			if punishment == "kick" then
				kickPlayer(targetPlayerElement)
				outputChatBox(targetPlayerName.." kicked from server! (Reason: "..description..")", root)
				
			elseif punishment == "ban" then
				banPlayer(targetPlayerElement)
				outputChatBox(targetPlayerName.." banned from server! (Reason: "..description..")", root)
			end
		else
			outputChatBox("target player not found", player)
		end
    else
    	outputChatBox("invalid rule id", player)
    end
end)

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...