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

Problems with number values?

Asked by 5 months 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 2055 Moderation Voter Community Moderator
5 months ago
Edited 5 months 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 164 — 5mo
0
so i remove the value? HappyTimIsHim 164 — 5mo
0
i changed line 14 (or 13 in your original script) because it was wrong gioni01 2055 — 5mo
0
yes, .Changed is called on the actual object, not the object's value gioni01 2055 — 5mo
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 746 — 5mo
1
notice how the passed parameter is the value name that changed: https://developer.roblox.com/api-reference/event/Instance/Changed gioni01 2055 — 5mo
0
i added an example gioni01 2055 — 5mo
1
good stuff gioni DinozCreates 746 — 5mo
0
thx dad. accept the answer if it helped tim, bc i saw you havent been accepting anyone's answers. gioni01 2055 — 5mo
1
Events are not called incapaxx 3172 — 5mo
0
give me ur number and ill call u gioni01 2055 — 5mo
Ad

Answer this question