Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

which would be better for this? table vs. datastores

I'm getting the Player's name and putting in a list that can be randomly called from all servers (old and new)

i know how to make both datastores and tables. what I don't know is which one would be better for this

example: I join my game and it sends my name to the list, meanwhile in another server, someone is walking (the way i have it to randomly pick a name) and they see my name.

I need it to save all player's name that join the game

local DS = game:GetService("DataStoreService"):GetDataStore("Players");   

game.Players.PlayerAdded:connect(function(plr)
	DS:UpdateAsync("PlayerListTest", function(oldTable)
        local newTable = oldTable
        if not newTable[plr.UserId] == true then
            table.insert(newTable, plr.UserId); -- This inserts the players userid into the table. That way if they changeg their name it will work still.

        end;

        return newTable
    end)
script.Parent.Touched:connect(function()
	
	for key, value in pairs(newTable) do   -- newTable has the blue line under it here
		
		local picked = math.random(key, value)
		print(value)    
	end
	end)
end)

4 comments

0

I believe that a datastore would work better for this.

0

You have to use datastore to do stuff like that. Not sure how fast datastore is, though, so it might not work great.

0

I mean, if you have your own web server, you could query to and from it with HttpService as well - if you know a little PHP :P Datastore is probably your best bet here.

0

I learned PHP and made a forum website when I was 10. xD

1 answer

✓ Accepted answer

The only way to do this is by using a DataStore. Your code would look something like this in the script adding the player to the DataStore:

local DS = game:Getservice("DataStoreService"):GetDataStore("Players");

game.Players.PlayerAdded:connect(funcion(plr)
	DS:UpdateAsync("PlayerListTest", function(oldTable)
		local newTable = oldTable;
		if not newTable[plr.UserId] == true then
			table.insert(newTable, plr.UserId); -- This inserts the players userid into the table. That way if they changeg their name it will work still.
		end;
		return newTable;
	end);
end);

Hope this helped! If it didn't feel free to ask any questions you have.

In your adding script:

local DS = game:GetService("DataStoreService"):GetDataStore("Players");   

game.Players.PlayerAdded:connect(function(plr)
    DS:UpdateAsync("PlayerListTest", function(oldTable)
        local newTable = oldTable
        if not newTable[plr.UserId] == true then
            table.insert(newTable, plr.UserId); -- This inserts the players userid into the table. That way if they changeg their name it will work still.

        end;

        return newTable;
    end);
end);

Seperate script:

local DS = game:GetService("DataStoreService"):GetDataStore("Players");   

script.Parent.Touched:connect(function()
	local newTable = DS:GetAsync("PlayerListTest");
	local uid = newTable[math.random(1, newTable)];
	local plr = game.Players:GetNameFromUserIdAsync(uid);
	print(plr);
end)

6 comments

0

this should work. i havent tested it yet

0

nevermind. it doesnt print the other person's UserId from a different server, and it only prints my UserId even in a new server

0

Are you querying the table from the other server?

0

i think so. although the name of the table has a blue line under it when i have it in my function. added the script

0

Did it work?

0

yea

This discussion is preserved as it was. New answers, comments, and votes are closed.