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

I have this data store script, but it doesn't work. Could someone help?

Here's the script I have for the data store.

local DataStoreService = game:GetService("DataStoreService")
local MBucksDataStore = DataStoreService:GetDataStore("MBucksDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "playerstats"
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "MBucks"
	
	local data
	local userID =  player.UserId
	
	local success, errormessage = pcall(function()
		data = MBucksDataStore:GetAsync(userID)
	end)	
	
	if success then
		Cash.Value = data
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local userID =  player.UserId
	
	local data = player.playerstats.MBucks.Value
	
	local success, errormessage = pcall(function()
		MBucksDataStore:SetAsync(userID, data)
	end)
	
	if success then
		print("Data succesfully saved.")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)

It won't even print the error message or warn the error message. There are no errors in the output, and I did change it in the server. Please help.

4 comments

0

are you in studio

0

Yes.

0

did you enable api services in studio

0

yes

1 answer

✓ Accepted answer

I had the same problem when i followed a script from a YouTube video. Try using this script instead, it worked for me.

local Players = game:GetService("Players"); local DataStoreService = game:GetService("DataStoreService");

local Datastore = DataStoreService:GetDataStore("MyDataStore");

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player);
    leaderstats.Name  - "leaderstats";
 
    local cash = instance.new("IntValue", leaderstats);
    cash.Name =  "Cash";
 
    local data = nil;
 
    local s, e = pcall(function()
        data = Datastore:GetAsync(player.UserId.."-cash")
    end)
 
    if(s) then
        print("data successfully loaded")
        data = data or 0 -- if no data has been saved, it replaces it with 0 for default
        cash.Value  = data
    else
        warn("There was a problem loading data:", e);
    end
end);
 
 
Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player.leaderstats
    local cash = leaderstats.Cash
 
    local s, e = pcall(function()
        Datastore:SetAsync(player.UserId.."-cash", cash.Value)
    end);
 
    if(s) then
        print("data successfully saved");
    else
        warn("There was a problem saving data:", e);
    end
end)

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