Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Problems with number values?

Asked by 17 days ago
DS = game:GetService("DataStoreService")
MaxKi = DS:GetDataStore("kiMaxSave")
game.Players.PlayerAdded:Connect(function(plr)
    print(plr.Name.."has joined the game")
    local stats = Instance.new("Folder", plr)
    stats.Name = "Stats"
    KiMax = Instance.new("NumberValue", stats)
    KiMax.Name = "KiMax"
    KiMax.Value = MaxKi:GetAsync(plr.UserId) or 0
    KiValue = Instance.new("NumberValue", stats)
    KiValue.Name = "KiValue"
    KiValue.Value = KiMax.Value 
    KiMax.Value.Changed:Connect(function()
        MaxKi:SetAsync(plr.UserId, KiMax) 
    end)
end)

I've made this and it says

  18:14:38.674 - ServerScriptService.Script:13: attempt to index field 'Value' (a number value)

as an error.

1 answer

Log in to vote
3
Answered by
gioni01 1494 Moderation Voter
17 days ago
Edited 17 days ago

.Changed is an event of Instance, but you used it on the actual value of the instance, which is a number.

DS = game:GetService("DataStoreService")
MaxKi = DS:GetDataStore("kiMaxSave")
game.Players.PlayerAdded:Connect(function(plr)
    print(plr.Name.."has joined the game")
    local stats = Instance.new("Folder") --second argument of .new() deprecated
    stats.Name = "Stats"
    stats.Parent = plr
    KiMax = Instance.new("NumberValue", stats)
    KiMax.Name = "KiMax"
    KiMax.Value = MaxKi:GetAsync(plr.UserId) or 0
    KiValue = Instance.new("NumberValue", stats)
    KiValue.Name = "KiValue"
    KiValue.Value = KiMax.Value 
    KiMax.Changed:Connect(function()
        MaxKi:SetAsync(plr.UserId, KiMax) 
    end)
end)

Here's another example of using the event:

game:GetService("Players").PlayerAdded:Connect(function(plr)
   local IntValue = Instance.new("IntValue")
   IntValue.Parent = plr
   IntValue.Changed:Connect(function(property)
     print(property)
   end)
   wait(3)
   IntValue.Value = 5 --will print the name of the property changed - "Value"
end)
0
???? HappyTimIsHim 122 — 17d
0
so i remove the value? HappyTimIsHim 122 — 17d
0
i changed line 14 (or 13 in your original script) because it was wrong gioni01 1494 — 17d
0
yes, .Changed is called on the actual object, not the object's value gioni01 1494 — 17d
View all comments (7 more)
0
As he stated, .Changed is looking for changes in the Value of an instance, so since it already looks to the value theres no need to reference it directly. DinozCreates 424 — 17d
1
notice how the passed parameter is the value name that changed: https://developer.roblox.com/api-reference/event/Instance/Changed gioni01 1494 — 17d
0
i added an example gioni01 1494 — 17d
1
good stuff gioni DinozCreates 424 — 17d
0
thx dad. accept the answer if it helped tim, bc i saw you havent been accepting anyone's answers. gioni01 1494 — 17d
1
Events are not called incapaxian 1315 — 17d
0
give me ur number and ill call u gioni01 1494 — 17d
Ad

Answer this question