Please help me fix this error!

Hello devs, I received an error in the output: “attempt to concatenate nil with string”. Could you please help me fix this issue? I don’t know what’s wrong with it. Any feedback would be appreciated. Thank you!

Script 1:

local TS = game:GetService("TeleportService")
local DSS = game:GetService("DataStoreService")

local event = game.ReplicatedStorage.Events.CreateServer
local e2 = game.ReplicatedStorage.Events.JoinServer

event.Event:Connect(function(creator, mapname)
    local tableforjoin = {}
    
    table.insert(tableforjoin, creator)
    
    local reserved = TS:ReserveServer(12607086809)
    local ID = 1
    local canID = false
    
    repeat
        if DSS:GetDataStore(ID):GetAsync("G") == nil then
            canID = true
            DSS:GetDataStore(string.sub(reserved, 1,8)):SetAsync("G", ID)
            DSS:GetDataStore(ID):SetAsync("G", creator.UserId)
            DSS:GetDataStore(ID .. "2"):SetAsync("G", reserved)
            DSS:GetDataStore(ID .. "3"):SetAsync("G", mapname)
        else
            ID += 1
        end
    until canID == true
    
    wait(.1)
    TS:TeleportToPrivateServer(12607086809, reserved, tableforjoin)
end)

e2.OnServerEvent:Connect(function(plr, id)
    local tableforjoin = {}

    table.insert(tableforjoin, plr)
    
    local searchserver = DSS:GetDataStore(id .. "2"):GetAsync("G")
    
    TS:TeleportToPrivateServer(12607086809, searchserver, tableforjoin)
end)

script 2:

local DSS = game:GetService("DataStoreService")

local reserved = game.PrivateServerId

local ID = DSS:GetDataStore(string.sub(reserved, 1,8)):GetAsync("G")
local mapname = DSS:GetDataStore(ID .. "3"):GetAsync("G")

local newmap = game.ReplicatedStorage.Maps:FindFirstChild(mapname):Clone()
game.Workspace:FindFirstChild("Map"):Destroy()
newmap.Parent = game.Workspace
newmap.Name = "Map"

game.Players.PlayerAdded:Connect(function(plr)
    plr.PlayerGui.ID.TextLabel.Text = ID
end)

output:

Line 6 attempt to concatenate nil with string
-- error from script 2

Any feedback would be appreciated. Thank you!

If you are trying to run script 2 in a non-reserved server then it’ll throw “” aka empty. But not nil!

Below is what roblox does as a check to see what type of server you’re currently in.

local function getServerType()
	if game.PrivateServerId ~= "" then
		if game.PrivateServerOwnerId ~= 0 then
			return "VIPServer"
		else
			return "ReservedServer"
		end
	else
		return "StandardServer"
	end
end

print(getServerType())

Essentially, not ever server you join will return a PrivateServerId

but, its reserved

and thank you for ur feedback!,

i hope it will be fixed!

Did you run the code in a reservedserver or a regular server?

if i run that script in command bar it says reserved server

and yes

the script 1 in regular

script 2 in reserved

Run this print(game.PrivateServerId) in command bar and tell me what it returns with.

Do you have script2 disabled until you join a server thats reserved?

it prints c065561d-eaaf-47f3-a864-dde9cadc2ec5
and the script 2 is runned in the reserved server

Awesome, with this in mind, can you then do a print(ID,mapname) and see what it returns with?

I’ve replicated your error on my end. It is ID that returns nil

the same error, idk why

it prints the same error…

Script - Roblox Studio (gyazo.com)

Here is what I got, Ill take a look and see whats going on.

So, the issue is because when you call “GetAsync” in script2 it calls the table; however, the table is showing as nil. This means the data that was stored/saved/updated using SetAsync in this table from script1 was most likely never saved properly.

GlobalDataStore | Roblox Creator Documentation

1 Like

Updated my comment. Ill take a look into seeing why your table doesnt save.

Join system work
but
yea
about the map loading of the create server
thats what not work

we are not saving the table as map name

we are saving a string

Alright, we can try wrapping your script1 when you call ‘SetAsync’ inside of a pcall.

I’d recommend something along these lines;

    local code = nil
	local success,err = pcall(function()
		code = DSS:GetDataStore(ID):SetAsync("G", creator.UserId)
	end)
    if success and code then
    end

we, dont use the creator anyore we only use mapname now.

To understand properly, we no longer get the error at line 6?

But we are now getting a different error?

we do and its about the map name

Alright, one thing I do notice in script1,

    local reserved = TS:ReserveServer(12607086809)
    local ID = 1
    local canID = false
    repeat
        if DSS:GetDataStore(ID):GetAsync("G") == nil then
            canID = true
            DSS:GetDataStore(string.sub(reserved, 1,8)):SetAsync("G", ID)
            DSS:GetDataStore(ID):SetAsync("G", creator.UserId)
            DSS:GetDataStore(ID .. "2"):SetAsync("G", reserved)
            DSS:GetDataStore(ID .. "3"):SetAsync("G", mapname)
        else
            ID += 1
        end
    until canID == true

If ID in script1 == 1 or 12 or 13, but in script2 that runs on the reservedserver is calling code as DSS:GetDataStore(string.sub(reserved, 1,8)):GetAsync("G"), which will return as a different string or number that compared to script1.

Because if you notice,

local ID = DSS:GetDataStore(string.sub(reserved, 1,8)):GetAsync("G")
local mapname = DSS:GetDataStore(ID .. "3"):GetAsync("G")

ID would not return 1 or 12 or 13 as it is SetAsynced in script1. Therefore, mapname would not exist and return nil.

the id system is working but the nil to string mapname is not

And, sorry for the confusion.

I dont understand

            DSS:GetDataStore(ID):SetAsync("G", creator.UserId)
            DSS:GetDataStore(ID .. "2"):SetAsync("G", reserved)
            DSS:GetDataStore(ID .. "3"):SetAsync("G", mapname)

This code from script 1 will never be the same be picked up by script2.

This is because the ID you are calling in script 2 is NOT the same as the one you call in script1.

The ID in script 1 will NOT be a reservedserver code/id. It is just a number that counts up (i.e 1,2,3,4, etc)

how do we fix it?

i am not sure how to fix this…