Unable to assign property string expected, got Instance

So i"m trying to make a function that makes a value that saves with the profile service, but it doesn’t work I keep getting this error:
Unable to assign property Name. string expected, got Instance

This is my script:

	local function makeValue(name, parent, sort)
		name = tostring(name)
		local name = Instance.new(sort)
		name.Name = name
		name.Value = profile.Data[name] or nil
		name.Parent = parent
		
		table.insert(saveTable,name)
	end

	makeValue("Cash","leaderstats","IntValue")

I also tested it wothout the tostring, and every argument prints if I print it.

This is coming out as an Instance. Can you send the code that makes that part?

I don’t really know wich part you mean, but I can sent you the script, it’s not that big.

local profileService = require(script:WaitForChild('ProfileService'))
local wkspace = game:GetService("Workspace")
local askRebirthEvent = game.ReplicatedStorage.Remotes.AskRebirth
local plots = wkspace:WaitForChild("Plots")
local templatePlot = wkspace:WaitForChild("TemplatePlot")

local profileTemplate = {
	cash = 0;
	cashPerSecond = 0;
	cashToCollect = 0;
	totalCash = 0;
	totalTime = 0;
	totalConversations = 0;
	totalEggs = 0;
	firstTime = true;
	Items = {};
}
local profileStore = profileService.GetProfileStore(
	"PlayerData5",
	profileTemplate
)

local profiles = {}

local function playerAdded(player)
	local profile = profileStore:LoadProfileAsync("Player_"..player.UserId)

	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()

		profile:ListenToRelease(function()
			profiles[player] = nil
			player:Kick()
		end)

		if player:IsDescendantOf(game.Players) == true then
			profiles[player] = profile
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

local saveTable = {}

game.Players.PlayerAdded:Connect(function(player)
	
	playerAdded(player)
	
	local profile = profiles[player]
	
	if not profile then warn("No profile bruh!") return end
	
	local function makeValue(name, parent, sort)
		local name = Instance.new(sort)
		name.Name = name
		name.Value = profile.Data[name] or nil
		name.Parent = parent
		
		table.insert(saveTable,name)
	end
	
	local leaderstats = Instance.new("Folder") 
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local values2 = Instance.new("Folder")
	values2.Name = "Values2"
	values2.Parent = player

	makeValue("Cash","leaderstats","IntValue")
	
	makeValue("CashPerSecond","Values2","IntValue")
	
	makeValue("CashToCollect","Values2","IntValue")
	
	makeValue("FirstTime","Values2","BoolValue")
	
	makeValue("TotalCash","Values2","IntValue")
	
	makeValue("TotalTime","Value2s","IntValue")
	
	makeValue("TotalConversations","Values2","IntValue")
	
	makeValue("TotalEggs","Values2","IntValue")
	
	for i,v in saveTable do
		if player.leaderstats:FindFirstChild(v) then
			player.leaderstats[v]:GetPropertyChangedSignal("Value"):Connect(function()
				profile.Data[v] = player.leaderstats[v].Value
			end)
		elseif player.Values2:FindFirstChild(v) then
			player.Values2[v]:GetPropertyChangedSignal("Value"):Connect(function()
				profile.Data[v] = player.Values2[v].Value
			end)
		end
	end
	
	game.ServerScriptService:WaitForChild("PlotHandler"):WaitForChild("CreatePlot"):Fire(player, profile.Data.Items)
end)


game.Players.PlayerRemoving:Connect(function(player)
	local profile = profiles[player]
	
	if profile then
		profile:Release()
	end
end)

for i, player in game.Players:GetPlayers() do
	task.spawn(playerAdded, player)
end

script:WaitForChild("ItemUnlocked").Event:Connect(function(player, itemId)
	local profile = profiles[player]
	table.insert(profile.Data.Items, itemId)
end)

hi, i noticed something that might be the problem

when you create a new instance of sort and assign it to the local variable name , you’re overwriting the name parameter of the function, which is supposed to be a string

heres an example that i made

local function makeValue(valueName, parent, sort)
    local valueObject = Instance.new(sort)
    valueObject.Name = valueName
    valueObject.Value = profile.Data[valueName] or 0
    valueObject.Parent = parent

    table.insert(saveTable, valueObject)
end

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