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
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
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.
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.