Why is my datastore not saving?

Im making a obby with saving stages. It some what works but it sometimes saves. I can up with a solution of when you step on the check point it autosaves to the players datastore. I thought this would be a good idea till I got this error: attempt to index function with ‘SetAsync’
I cant find anyway to fix it please help.

How Im Saving:

local DataStoreService = game:GetService("DataStoreService")
local saveStage = DataStoreService:GetDataStore("Stages")

function saveStage(player)
	local success, errormessage = pcall(function()
		saveStage:SetAsync(player.UserId.."-stages", player.StageOn.Value)
	end)

	if success then
		print("Player Stage Was Saved!")
	else
		print("There Was A Problem Getting Player Stage!")
		warn(errormessage)
	end
end

This function calls when the part is touched and is 1 stage higher then before

I highly suggest using @Alvin_Blox 's video and modifying the script to your needs. Roblox DataStore Tutorial - Data Stores & Saving Data - Scripting Tutorial - YouTube

Also watch the video the full way through to see what he’s doing and why. It’s probably not the best to just skip ahead.

You are setting the value but you never extract it from the data store. You have to use the GetAsync() function to get the data and then set the StageOn value to the datastore value.

You’re literally attempting to define both a variable, and a function together

Rename your function or your GetDataStore name

local DataStoreService = game:GetService("DataStoreService")
local saveStage = DataStoreService:GetDataStore("Stages")

function save(player)
	local success, errormessage = pcall(function()
		saveStage:SetAsync(player.UserId.."-stages", player.StageOn.Value)
	end)

	if success then
		print("Player Stage Was Saved!")
	else
		print("There Was A Problem Getting Player Stage!")
		warn(errormessage)
	end
end

yep thats what I am using
(30 )

1 Like

lol, didn’t see that xD. Thanks I’m literally brain dead after doing so much stuff with datastores lol.