Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to use GlobalData Store?

Asked by
ikiled 75 Donator
8 years ago

When using

:GetDataStore()

things save completely fine.

When using

:GetGlobalDataStore()

local Data = {}
local Http = game:GetService("HttpService")
--------
local DS = game:GetService("DataStoreService")
local CodeDS = DS:GetGlobalDataStore("asdasd333334dsf")

--------
local RepStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

function Data:CodeRedeemed(userId, code)
wait()
CodeDS:SetAsync(tonumber(userId).."_"..code, "Redeemed") 
warn'SET'
end
function setCodes(player, code)
if Data:CheckIfRedeemedCode(player.userId, code) then
warn'Code already redeemed'
else
Data:CodeRedeemed(player.userId, code) warn'Redeeming Code'
end 
end

function Data:CheckIfRedeemedCode(userId, code)
    if CodeDS:GetAsync(tonumber(userId).."_"..code) == "Redeemed" then
        return true
    else
        return false
    end
end

game.Players.PlayerAdded:connect(function(player)
    wait()
    if Data:CheckIfRedeemedCode(player.userId, "230-823-392") then
        warn'WHAT THE adsaddddddd'
    end
end)




game.ReplicatedStorage.code.OnServerEvent:connect(setCodes)

What am I doing wrong here?

0
Tricky Shawnyg 4330 — 8y
0
Anyone :o ikiled 75 — 8y
0
Lol, I was hoping someone could answer so that I could understand this better as well. User#11440 120 — 8y
0
come on people... ikiled 75 — 8y

1 answer

Log in to vote
0
Answered by
bry0320 10
8 years ago

You're calling a function too early, specifically the function setCodes in line 16 is calling CheckIfRedeemedCode.

function setCodes(player, code)
if Data:CheckIfRedeemedCode(player.userId, code) then

However, in line 24...

function Data:CheckIfRedeemedCode(userId, code)

Functions that are deeper in the code can't be called by functions that are 'in the surface' of the code.

Lua isn't multi-threaded. It goes line by line. If it encounters a function not defined yet, it will break the whole script. Move the function CheckIfRedeemedCode to a line 16 or higher but below line 5 and the script might work again.

Ad

Answer this question