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

Will this work to signify if a player has been in the game before?

Asked by
unmiss 337 Moderation Voter
8 years ago

If they haven't been in the game before, this will set it to true, but something else needs to happen if they haven't, my intro GUI handled by a separate localscript. How exactly would I get this to work? I'm sort of being braindead at the moment and confused. Help, please?

local player = script.Parent.Parent.Parent.Parent
local datastoreservice = game:GetService("DataStoreService"):GetDataStore("BoE_BeenBefore")
local beenbefore


game.Players.PlayerAdded:connect(function()
    if datastoreservice:GetAsync(player.userId) == false or nil then
        script.Parent.beenbefore.Value = false
        datastoreservice:SetAsync(player.userId,true)
    elseif datastoreservice:GetAsync(player.userId) == true then
        script.Parent.beenbefore.Value = true
    end
end)

1 answer

Log in to vote
1
Answered by 8 years ago

Couple of things:

  • The key must not be the player's userId
  • You must either set or update async to set it.

Key

The key of a datastore must not be the player's userId; plain. So you can add a "player_" or "user_" in front of the id. Also, make sure the userId is greater than 0, this means the player is a guest.

if plyr.userId > 0 then
    DS:SetAsync("user_"..plyr.userId, 173821)
end

UpdateAsync(string Key, Variant function(Variant) transformFunction)

Updates the async.

DS:UpdateAsync("user_"..plyr.userId, function()
    return true --return the value you want it to change to.
end)

SetAsync(string Key, Variant value)

SetAsync is like GetAsync but there is another argument, the value you are setting it to.

DS:SetAsync("user_"..plyr.userId, 173821)


Final Product

local datastoreservice = game:GetService("DataStoreService"):GetDataStore("BoE_BeenBefore")

game.Players.PlayerAdded:connect(function(player) --You didn't need line 1, just do this instead.
    if datastoreservice:GetAsync("user_"..player.userId) == nil then
        datastoreservice:SetAsync("user_"..player.userId, false)
    elseif datastoreservice:GetAsync("user_"..player.userId) == false then
        datastoreservice:SetAsync("user_"..player.userId, true)
    end
end)


Hope it helps!

Ad

Answer this question