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

Making an item only add 1 to your stats once?

Asked by 5 years ago
Edited 5 years ago

I want to be able to have the players only obtain this orb once, but when i run this code and try to have it save it errors out

local DataStoreService = game:GetService("DataStoreService")

local orb1datastore = DataStoreService:GetDataStore("Orb1DataStore")



game.Players.PlayerAdded:Connect(function(player)

local orb1value = Instance.new("IntValue")

orb1value.Name = "orb1"

orb1value.Parent = player

local data

local success, errormessage pcall(function()

data = orb1datastore:GetAsync(player.UserId.."orb1")

end)

if success then

orb1value.Value = data

else

print("There was an error with getting your orb1 data")

warn(errormessage)

end

end)



game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage pcall(function()

orb1datastore:SetASync(player.UserId.."orb1",player.orb1.Value)

end)

if success then

print("Orb1 data successfully saved")

else

print("There was an error while saving orb1 data")

warn(errormessage)

end

end)

1 answer

Log in to vote
0
Answered by 5 years ago

I believe this will work:

   local DataStoreService = game:GetService("DataStoreService")

local orb1datastore = DataStoreService:GetDataStore("Orb1DataStore")



game.Players.PlayerAdded:Connect(function(player)

local orb1value = Instance.new("IntValue")

orb1value.Name = "orb1"

orb1value.Parent = player

local data

local success, errormessage pcall(function()

data = orb1datastore:GetAsync(player.UserId.."orb1")

end)

if success then

orb1value.Value = data

else

print("There was an error with getting your orb1 data")

warn(errormessage)

end

end)



game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage pcall(function()
if player:FindFirstChild("orb1") then else print("Ok your values are noobs") return end

orb1datastore:SetASync(tostring(player.UserId).."orb1",player:FindFirstChild("orb1").Value)

end)

if success then

print("Orb1 data successfully saved")

else

print("There was an error while saving orb1 data")

warn(errormessage)

end

end)
0
It still errors saying its nil spot6003 64 — 5y
Ad

Answer this question