How can I insert index into table as a string?

Also, are you using PlayerNames.AddPlayerName from the server or client?
You should be using the server for that method. Also I am attempting to write a better version of the script and I will send it soon

1 Like

So I finished the new rewritten script to prevent exploiting of events multiple times. It will allow you to set values of events so you can verify serverside if a event already happened. You could do this for every event individually, but since you wanted a module script then here it is:

local module = {}

--##########--
-- HappyFloof1
--##########--

--Remember to only use these event as server side validation to prevent exploiting. Also only set the event names from the server. Values that the clients set aren't replicated to the server and other players.

local Players = {
	
}

game.Players.PlayerAdded:Connect(function(player)
	Players[player.Name] = {}
	--print("Added player to list")
end)

game.Players.PlayerRemoving:Connect(function(player)
	if table.find(Players, player.Name) then
		table.remove(Players, table.find(Players, player.Name))
		--print("Removed player from list")
		--print(Players)
	end
end)

function module.CheckEvent(playerName, EventName)
	
	if Players[playerName] then
		if Players[playerName][EventName] then
			return Players[playerName][EventName]
		end
	end
	
	return false
end

function module.SetEvent(playerName, EventName, Value)
	if Value == nil then Value = true end
	--print(Players)
	if Players[playerName] then
		Players[playerName][EventName] = Value
		--print(Players)
		return true
	end
	return false
end

return module

This scripts needs to be put in replicated storage or server script service.
Remember that the CheckEvent function and the SetEvent function need to be ran from server side because the values don’t replicate.

Use module.SetEvent(playerName, EventName, Value) to set values of events and use CheckEvent(playerName, EventName) to check event values.

Use this from serverside otherwise it won’t work properly

From the server client will never touch the module

Okay I have understand every part except the Value in SetEvent what is that thing exactly?

The Value in SetEvent is used is an optional parameter I put to set the value as true by default, so when you use check event it will respond with true, while if you put false as the Value, it will set the event value to false, so when you use CheckEvent, it will return false. This is so you can reset the value. You can also edit the module to add RemoveEvent or change Value so that when Value == false, Players[playerName][EventName] = nil in the module.SetEvent function.

local playerData = {
    ["Player1"] = {
        ["Hats"] = {};
        ["Uniforms"] = {};
        ["Classes"] = {};
    };
    ["Player2"] = {
        ["Hats"] = {};
        ["Uniforms"] = {};
        ["Classes"] = {};
    };
    ["Player3"] = {
        ["Hats"] = {};
        ["Uniforms"] = {};
        ["Classes"] = {};
    };
}

local Players = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
    Players[player.Name] = true -- use the player name as the key and a boolean value to indicate they are online.
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    Players[player.Name] = nil
end)

local PlayerNames = {}

function PlayerNames.RemovePlayerName(EventName, player)
    local playerDataEntry = playerData[player.Name]
    if playerDataEntry then
        playerDataEntry[EventName] = nil
        print("Removed player from the event: " .. EventName)
    end
end

function PlayerNames.AddPlayerName(EventName, player)
    local playerDataEntry = playerData[player.Name]
    if playerDataEntry then
        playerDataEntry[EventName] = true
        print("Added player to the event: " .. EventName)
    end
end

function PlayerNames.CheckInTheTable(EventName, player)
    local playerDataEntry = playerData[player.Name]
    return playerDataEntry and playerDataEntry[EventName]
end

try this

If this doesnt work you can always use metatables to write complex data in to your tables or read complex datas

__index to read datas
__newindex to write datas

1 Like

Sounds interesting but you have misunderstood, the playerData table was an example, the table should be written using scripts and functions only not manually

1 Like

Indexes must be unique, so if you don’t want to overwrite old data, place the duplicates in an array:

local Players = {
	Classes = {Player1, Player2, Player3, Player5},
	Uniforms = {Player4},
	Hats = {Player6}
}

That is my whole issue right now how to add multiple values to index without resetting it?

Instead of adding a single value, make the value an array as shown above and insert the values to that array. Basically to insert them do table.insert(Players[index], value).

So it will be something like this?

local Players = {
Classes = {},
Uniforms = {},
Hats = {}

function PlayerNames.AddPlayerName(EventName, player)
      table.insert(Players[EventName], player.Name)
end

Something like this?

Yeah exactly you can use that i was gonna reply that but i was busy so yeah i had you on my list if you wanna remove tables too you can use this:

function PlayerNames.RemovePlayerName(EventName, player)
    for i, playerName in ipairs(Players[EventName]) do
        if playerName == player then
            table.remove(Players[EventName], i)
            break
        end
    end
end

But why remove “i” if playerName == player? why not

table.remove(Players[EventName], playerName)

oh ive accidentally put “i” there sorry i didnt sleep last night i guess i was confused there

1 Like

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