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

Why is data persistence giving me an error when saving?

I am writing a script that bans players from the, while also saving the reason, so it can be displayed to the player, anyway, loading the data goes fine, no errors... but when saving it, it give me this error: ServerScriptService.loadUserData:19: bad argument #2 to '?' (string expected, got Object).

The code that I am using is:

game.Players.PlayerAdded:connect(function(player)
	
	player:WaitForDataReady()
	
	local userData = Instance.new("Folder", player)
	userData.Name = "userData"
	
	local banData = Instance.new("Folder", userData)
	banData.Name = "banData"
	
	local banReason = Instance.new("StringValue", banData)
	banReason.Name = "reason"
	banReason.Value = player:LoadString("banReason")
	
end)

game.Players.PlayerRemoving:connect(function(player)
	
	game.Players[player]:SaveString("banReason", player.userData.banData.banReason.Value)	
	
end)

Thanks!

1 comment

1

Don't use data persistance. Use DataStores. They're much better.

1 answer

✓ Accepted answer

bad argument #2 to '?' (string expected, got Object) means you indexed an value with the wrong type.This means that it should either be a string value not an object value or an object value not a string value

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