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 6 years ago
Edited 6 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

01local DataStoreService = game:GetService("DataStoreService")
02 
03local orb1datastore = DataStoreService:GetDataStore("Orb1DataStore")
04 
05   
06 
07game.Players.PlayerAdded:Connect(function(player)
08 
09local orb1value = Instance.new("IntValue")
10 
11orb1value.Name = "orb1"
12 
13orb1value.Parent = player
14 
15local data
16 
17local success, errormessage pcall(function()
18 
19data = orb1datastore:GetAsync(player.UserId.."orb1")
20 
21end)
22 
23if success then
24 
25orb1value.Value = data
26 
27else
28 
29print("There was an error with getting your orb1 data")
30 
31warn(errormessage)
32 
33end
34 
35end)
36 
37   
38 
39game.Players.PlayerRemoving:Connect(function(player)
40 
41local success, errormessage pcall(function()
42 
43orb1datastore:SetASync(player.UserId.."orb1",player.orb1.Value)
44 
45end)
46 
47if success then
48 
49print("Orb1 data successfully saved")
50 
51else
52 
53print("There was an error while saving orb1 data")
54 
55warn(errormessage)
56 
57end
58 
59end)

1 answer

Log in to vote
0
Answered by 6 years ago

I believe this will work:

01   local DataStoreService = game:GetService("DataStoreService")
02 
03local orb1datastore = DataStoreService:GetDataStore("Orb1DataStore")
04 
05   
06 
07game.Players.PlayerAdded:Connect(function(player)
08 
09local orb1value = Instance.new("IntValue")
10 
11orb1value.Name = "orb1"
12 
13orb1value.Parent = player
14 
15local data
16 
17local success, errormessage pcall(function()
18 
19data = orb1datastore:GetAsync(player.UserId.."orb1")
20 
21end)
22 
23if success then
24 
25orb1value.Value = data
26 
27else
28 
29print("There was an error with getting your orb1 data")
30 
31warn(errormessage)
32 
33end
34 
35end)
36 
37   
38 
39game.Players.PlayerRemoving:Connect(function(player)
40 
41local success, errormessage pcall(function()
42if player:FindFirstChild("orb1") then else print("Ok your values are noobs") return end
43 
44orb1datastore:SetASync(tostring(player.UserId).."orb1",player:FindFirstChild("orb1").Value)
45 
46end)
47 
48if success then
49 
50print("Orb1 data successfully saved")
51 
52else
53 
54print("There was an error while saving orb1 data")
55 
56warn(errormessage)
57 
58end
59 
60end)
0
It still errors saying its nil spot6003 64 — 6y
Ad

Answer this question