Attempt to concatenate number with nil

im currently creating a datatstore system that saves a table and these errors are coming out. im currently using a modulescript for the savedata and getdata functions:

function module.saveData(plr: Player, dataStore: DataStore, keyAddition:string, dataToSave)
	local success, err = pcall(function()
		print(keyAddition.."<br>"..dataToSave)
		dataStore:SetAsync(plr.UserId..keyAddition, dataToSave)
	end)
	
	if success then
		print("Successfully saved Data.")
	else
		warn(err)
	end
end

function module.getData(plr: Player, dataStore: DataStore, key)
	local data = {}
	
	local success, err = pcall(function()
		data = dataStore:GetAsync(plr.UserId..key)
	end)

	if success and data ~= nil then
		return dataStore
	elseif not success then
		warn(err)
	else
		print("Player is new. Creating new Data")
	end
end

when i call these functions they print a warning:

ReplicatedStorage.Data/Tables:35: attempt to concatenate number with nil 
ReplicatedStorage.Data/Tables:20: attempt to concatenate string with table 

Please show the entire module script, I cannot tell which lines are erroring.

thats basically the modulescript. the errors its reffering to are the

if not success then 
     warn(err)
end

Concatenating is linking multiple sets of characters together into one. In Lua, you can only concatenate strings and numbers. You can concatenate two variables by joining them together with a ..

print(keyAddition.."<br>"..dataToSave)

Since dataToSave is a table, Roblox throws an error when you try to concatenate it. The second error “attempt to concatenate number with nil” is most likely a result of keyAddition not existing, therefore it returns nil.

Ah, I’ve found the error, you can’t concatenate strings and tables together.

Try changing it to

print(keyAddition, dataToSave)

It prints the ‘keyAddition’ then the ‘dataToSave’ afterwards!

1 Like

what about the GetData function?
it still prints ReplicatedStorage.Data/Tables:20: attempt to concatenate string with table

EDIT: i just found a fix for it and its just the data variable. i basically just changed it from local data = {} to local data

EDIT AGAIN: the fix did NOT work.

bumping cause i still havent found a fix for the second error

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