DataStore script dont work in other place?

(sry for bad English)

I have a DataStore script that saves Bool Values
I tested it and it works
Then i put the script in an other place and it didnt worked anymore why?

This is the script :

local DSS = game:GetService(“DataStoreService”)

local datastore = DSS:GetDataStore(“WeaponSkinSave”, “Players”)

function generateDataKey(player)
local ret = “uid_” … player.userId
return ret
end

function generateDataTable(player)
local dataTable = {
Points = player:WaitForChild(“PlayerGui”):WaitForChild(“WeaponSkins”).Rainbow.Owned.Value
}
return dataTable
end

function saveDataForPlayer(player)
local key = generateDataKey(player)
local data = generateDataTable(player)
datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
player:WaitForChild(“PlayerGui”):WaitForChild(“WeaponSkins”).Rainbow.Owned.Value = data.Points
end

function loadDataForPlayer(player)
local key = generateDataKey(player)
local data = datastore:GetAsync(key)
inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:connect(function(player)
loadDataForPlayer(player)
player:WaitForChild(“PlayerGui”):WaitForChild(“WeaponSkins”).Rainbow.Owned.Changed:connect(function()
saveDataForPlayer(player)
end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer)

btw i activated API Services

Output :

ServerScriptService.DataStore:24: attempt to index local ‘data’ (a nil value) 21:04:44.100 - Stack Begin 21:04:44.100 - Script ‘ServerScriptService.DataStore’, Line 24 - global inputDataToPlayer 21:04:44.101 - Script ‘ServerScriptService.DataStore’, Line 30 - global loadDataForPlayer 21:04:44.101 - Script ‘ServerScriptService.DataStore’, Line 34 21:04:44.101 - Stack End

For other place> Does this mean that you have a game with places? If not, DataStore cannot be cross-compatible between different games, you’d have to have your own web hosting to store data, or store them on a spreadsheet, which is a bit more complex than just DS…

i mean
I put the script in a other game

(i created the script in a testing place and tested it then i put it in the gamer where i want to have it)
sry for bad english :c

Make sure it is published. If it isn’t it will produce errors. Also, if API access is not enabled, then it won’t work in studio.

its published and api access is enabled too it still not work

Ok. Could you upload the script itself so I can test it?

what do you mean?
the script is above

It’s a bit hard to read when you copy-paste it into an article.
Usually when you need help with code, you should upload it or format it correctly to make it easy to read.
Try uploading the file of the script for everybody.

local DSS = game:GetService("DataStoreService")

local datastore = DSS:GetDataStore("WeaponSkinSave", "Players")

function generateDataKey(player)
    local ret = "uid_" .. player.userId
    return ret
end

function generateDataTable(player)
    local dataTable = {
       Points = player:WaitForChild("PlayerGui"):WaitForChild("WeaponSkins").Rainbow.Owned.Value
    }
    return dataTable
end

function saveDataForPlayer(player)
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key,data)
end

function inputDataToPlayer(player, data)
  player:WaitForChild("PlayerGui"):WaitForChild("WeaponSkins").Rainbow.Owned.Value = data.Points
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

game.Players.PlayerAdded:connect(function(player)
    loadDataForPlayer(player) 
    player:WaitForChild("PlayerGui"):WaitForChild("WeaponSkins").Rainbow.Owned.Changed:connect(function()
        saveDataForPlayer(player) 
    end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer)

Found the issue. At line 30 you try to call function inputDataToPlayer. If you print it, I bet it would return nil for the data variable. That means you are storing the data incorrectly (or so it seems). It’s a bit weird that it worked somewhere else. Keep in mind that you can only save integers, so make sure that is what you are doing.

TL;DR: You don’t save your data properly.

Do you know how to fix it?

Bec i dont know what to do next xDD

Are you saving strings? That could be a source of error; you can’t save strings, only numbers.

im saving booleans

but i found a way:

i hide the leaderboard
create new leaderstats named (RainbowGunSkin)
and my leaderstats save script will save them xD

Is that a stupid idea?

I don’t know tbh :man_shrugging:
Like I said, you can only save numbers. If you want to save a true/false thing, use 0s and 1s to do the job

kk
Thanks for your help!
I will try it xD

1 Like

No, this information is false. You can save any valid Lua primitive type except functions, threads and userdata. That’s nil, strings, numbers, booleans and tables.

OP’s issue is that their code assumes that all code returns truthy values and doesn’t handle any errors of any kind, especially if no data is returned from a GetAsync call. This is something that can be fixed through a general code skim and confirming what is being saved.

cc @itz_rennox

5 Likes

Cframe and objects can not be saved

If what you mean is,
You created a DS script, for a specific place, which save player stats let’s say, and you want the same script to work to a different place to load the stats to another game, based on the game you have the DS script(Basically Cross Compatible) You will need to web host a data store. You can’t do that just by placing the same script inside another game you made.

Maybe try keeping the DataStore script in different places in the same universe, not different games.

Because places in a game are all connected, they can share the same DataStore, because starter places (games) are not connected, they cannot share the same DataStore.

Yes, that was mentioned.

CFrames and Instances are userdata. You can verify this by using the Lua Global type on an object. The Roblox Global typeof will return Roblox and Lua primitive datatypes.