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

Why does the script think that it's a bool value when I need a Integer value?

Asked by
mine248 40
8 years ago

I have tried to edit a script, so that there can be handling more stats (removed as it's not the main idea). But when I test it, it would say

19:18:51.212 - ServerScriptService.Script:27: attempt to perform arithmetic on local 'oldValue' (a boolean value)

19:18:51.213 - Stack Begin

19:18:51.215 - Script 'ServerScriptService.Script', Line 27

19:18:51.216 - Stack End

Are there any solutions for getting rid of this error? Or is it ROBLOX bug?

Faulty code:

local replicated = game:GetService("ReplicatedStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("essintials")


game.Players.PlayerAdded:connect(function(player)
    --create leadstats
    local leadstats = Instance.new("IntValue")
    leadstats.Name = "leaderstats"
    leadstats.Parent = player
    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0
    money.Parent = leadstats
    local exp = Instance.new("IntValue")
    exp.Name = "Experience"
    exp.Parent = leadstats
    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Parent = leadstats
    local team = Instance.new("StringValue")
    team.Name = "Team"
    team.Value = "None (Deciding)"
    --datastore
    local key = "user_" .. player.userId
    --increase by 100 money per entering
    DataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue + 100
        local addedValue = oldValue == newValue
        money.Value = addedValue
        return addedValue
    end)
end)

1 answer

Log in to vote
0
Answered by
Legojoker 345 Moderation Voter
8 years ago

Well, first of all, because your oldValue was already set as a boolean (because of line 28, I'll get to this in a second), your script on line 27 will have a problem trying to add a true/false value to 100...obviously.

Now then, the reason for this problem is because on line 28, you address local addedValue as a boolean; oldValue == newValue is testing to see whether the two values are equal. If they are equal, addedValue = true, if they aren't, addedValue = false. This is a very useful feature in LUA to make code more efficient when you want a variable to be the result of a comparison, much like the comparisons in if statements.

Now then, I'm not really sure what you're trying to do on line 28. I assume you're making addedValue = the oldValue's number + 100, but you could just use newValue. I hope this helped.

Ad

Answer this question