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

Why Is this printing the same thing?

I have some code that should be making it so that each player that joins gets a different "IP" but every player gets the same one...

My code:

math.randomseed(tick() % 1 * 1e7) --I took some of your code and tried to make it work with mine
 
local t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30 --defining variables
t = 0
_G.UIP = {t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30} --asigning variables
local StaticLocalIP

function randomIP()
    return math.random(0, 255)
        .. "." .. math.random(0, 255)
        .. "." .. math.random(0, 255)
        .. "." .. math.random(0, 255)
end

function ipTaken(ip)
    for _, value in ipairs(_G.UIP) do
        if value == ip then
            return true
        end
    end
end

function uniqueIP() --makes unique IP
    local StaticLocalIP
    repeat
        StaticLocalIP = randomIP()
    until not ipTaken(StaticLocalIP)
    table.insert(_G.UIP, StaticLocalIP)
    return StaticLocalIP
end

StaticLocalIP = uniqueIP() -- assigns variable for rest of script 

game.Players.PlayerAdded:connect(function(player)
	print(player.Name.. " Has joined and is asigned the following IP: ".. StaticLocalIP)--prints some info
	
	t = t + 1	
				--I know there is a better way to do this:
	if t==1 then
		t1 = StaticLocalIP 
	elseif t==2 then
		t2 = StaticLocalIP
	elseif t==3 then
		t3 = StaticLocalIP
	elseif t==4 then
		t4 = StaticLocalIP
	elseif t==5 then
		t5 = StaticLocalIP
	elseif t==6 then
		t6 = StaticLocalIP
	elseif t==7 then
		t7 = StaticLocalIP
	elseif t==8 then
		t8 = StaticLocalIP
	elseif t==9 then
		t9 = StaticLocalIP
	elseif t==10 then
		t10 = StaticLocalIP
	elseif t==11 then
		t11 = StaticLocalIP
	elseif t==12 then
		t12 = StaticLocalIP
	elseif t==13 then
		t13 = StaticLocalIP
	elseif t==14 then
		t14 = StaticLocalIP
	elseif t==15 then
		t15 = StaticLocalIP
	elseif t==16 then
		t16 = StaticLocalIP
	elseif t==17 then
		t17 = StaticLocalIP
	elseif t==18 then
		t18 = StaticLocalIP
	elseif t==19 then
		t19 = StaticLocalIP
	elseif t==20 then
		t20 = StaticLocalIP
	elseif t==21 then
		t21 = StaticLocalIP
	elseif t==22 then
		t22 = StaticLocalIP
	elseif t==23 then
		t23 = StaticLocalIP
	elseif t==24 then
		t24 = StaticLocalIP
	elseif t==25 then
		t25 = StaticLocalIP
	elseif t==26 then
		t26 = StaticLocalIP
	elseif t==27 then
		t27 = StaticLocalIP
	elseif t==28 then
		t28 = StaticLocalIP
	elseif t==29 then
		t29 = StaticLocalIP
	elseif t==30 then
		t30 = StaticLocalIP
	end
	_G.UIP = {t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30} --reasignes table
	for index, value in ipairs(_G.UIP) do --prints whole table
	print(index, '=', value)
end
end)

game.Players.PlayerRemoving:connect(function(player) --when the player leaves resets a bit
    t = t - 1
end)

does anyone know why this is giving everyone the same "IP"

I still have a lot of work to don't worry I know like with the player removing function

2 answers

Randomseed

Don't use randomseed more than once. Just use it once at the beginning of your script. Further, you won't get good results using tick() because the number is too large.

Use something like math.randomseed(tick() % 1e7) or math.randomseed(tick()%1 * 1e7)


math.random

math.random has a way to produce integers.

  • math.random(high) produces a random number between 1 and high, inclusive.
  • math.random(low, high) produces a random number between low and high, inclusive.
try = math.random(0, 255)
	.. "." .. math.random(0, 255)
	.. "." .. math.random(0, 255)
	.. "." .. math.random(0, 255)

There is no reason to define any of the locals that you have on lines 7 to 15. Define as late as possible to make your code simpler.


Format

Tab your code correctly. (And include your whole script -- or at least a syntatically-valid-portion of it -- when you post a question)

math.randomseed(tick() % 1 * 1e7)


local t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30
t = 0
_G.UIP = {t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30}
local YourCNP

local try = rndm11.. ".".. rndm22.. ".".. rndm33.. ".".. rndm44
for _, value in ipairs(_G.UIP) do
	if try == value then
		while true do
			try = math.random(0, 255)
				.. "." .. math.random(0, 255)
				.. "." .. math.random(0, 255)
				.. "." .. math.random(0, 255)
			if try == value then
				print("Failed")
			else
				break
			end
			wait()
		end
	end
end

local StaticLocalIP = try

game.Players.PlayerAdded:connect(function(player)
	print(player.Name.. " Has joined and is asigned the following IP: ".. StaticLocalIP)
end)

This script is much easier to understand.

Looping Logic

Right now, your use of loops is not reasonable. Your loop doesn't actually check every one for each random identifier generated.

I highly suggest making your programs as simple and readable as possible.

The algorithm you are intending to use is this:

local ip
repeat
	ip = randomIP()
until not ipTaken(ip)
table.insert(_G.UIP, ip)

So make that work!

math.randomseed(tick() % 1 * 1e7)

_G.UIP = {}

function randomIP()
	return math.random(0, 255)
		.. "." .. math.random(0, 255)
		.. "." .. math.random(0, 255)
		.. "." .. math.random(0, 255)
end

function ipTaken(ip)
	for _, value in ipairs(_G.UIP) do
		if value == ip then
			return true
		end
	end
end

function uniqueIP()
	local ip
	repeat
		ip = randomIP()
	until not ipTaken(ip)
	table.insert(_G.UIP, ip)
	return ip
end

game.Players.PlayerAdded:connect(function(player)
	print(player.Name.. " Has joined and is asigned the following IP: ".. uniqueIP())
end)

ts

Your use of 30 t variables doesn't make any sense.

Just use the array, which you're starting out empty, anyway. Add with table.insert and table.remove when needed.

2 comments

0

I have Changed it BlueTaslem

0

And it still prints the same thing for every person

Roblox does not allow you to tell the IP of a user, for security reasons, that would work in the LUA compiler, but not roblox.

2 comments

0

The purpose of my code IP is to give each player a unique identifier that can be used to reference the player in-game, it is not there actual IP

0

sorry did not know

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