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

How do I add two variables together?

Asked by
4d0z 4
4 years ago

I'm trying to add two different variables together but nothing works out.

Here's the script. Look at lines 16-20

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    -- Display an 'IntValue' on leaderboard
    local Power = Instance.new("IntValue")
    Power.Name = "Power"
    Power.Value = 150
    Power.Parent = leaderstats

player.CharacterAdded:connect(function(Character)
    local Humanoid = Character:WaitForChild("Humanoid")
    Humanoid.Died:connect(function()
        if Humanoid:FindFirstChild("creator") ~= nil then
            local Killer = Humanoid.creator.Value
            local killerpoints = Killer.leaderstats.Power.Value
            local deadpoints = Power.Value
            killerpoints = killerpoints + deadpoints -- Add  dead players points to killer's points 
        end
         Power.Value = 0 --Set dead player's points to zero
end)
end)
0
remove the .Value part from the variable. Then when you add them, add the .Value there. This might work zblox164 531 — 4y
0
I said to change it for lines 16 and 17, not 15, 15 should stay the same theking48989987 2147 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago

Reference vs Value


A mistake that you are making is setting a variable to the value of a variable, rather than making a reference to the variable, and using the Value property of it after that.

Ex:

local numval = Number.Value--x
numval = numval + 2

print(Number.Value,numval,numval == Number.Value) --x, x+2, false

Ex2:

local num = Number--value is x
num.Value = num.Value + 5

print(num.Value)--x+5

Why is this? Well, it is because you are merely creating a number equivalent to the value of the Power of Both players, rather than referencing them. therefore, if you modify this new variable, the originals remain unchanged.


Fix


What you should do is remove the Value getter on lines 16 and 17, then add them on line 18 like such:

local killerpoints = Killer.leaderstats.Power
local deadpoints = Power
killerpoints.Value = killerpoints.Value + deadpoints.Value

small note:the deadpoints variable however, can remain unchanged, as no change is happening to it

Hopefully this helped! Besure to accept if it did!

0
Oh we have the same explanation lmao Mr_Unlucky 1085 — 4y
0
But yours is better  Mr_Unlucky 1085 — 4y
0
Thanks for the help! I've been working on this script for like a week and I haven't figured out what I want it to do 4d0z 4 — 4y
0
So I did what you said to do but I'm getting an error stating leaderstats is not a valid member of ObjectValue /// Any ideas?? 4d0z 4 — 4y
Ad
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
4 years ago

Its at line 17. Since you're defining it as the actual value rather then just doing the instance and then apply its value, you're only adding numbers but not the actual value.

Example:

If the enemy killerpoints is 50 and the variable you made is still like that then its just 50, and since the deadpoints variable is worth something like 70 then its just 120, and its not going to add anything to killerpoints.

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    -- Display an 'IntValue' on leaderboard
    local Power = Instance.new("IntValue")
    Power.Name = "Power"
    Power.Value = 150
    Power.Parent = leaderstats

player.CharacterAdded:connect(function(Character)
    local Humanoid = Character:WaitForChild("Humanoid")
    Humanoid.Died:connect(function()
        if Humanoid:FindFirstChild("creator") ~= nil then
            local Killer = Humanoid.creator.Value
            local killerpoints = Killer.leaderstats.Power
            local deadpoints = Power.Value
            killerpoints.Value = killerpoints.Value + deadpoints -- Add  dead players points to killer's points 
        end
         Power.Value = 0 --Set dead player's points to zero
end)
end)

Answer this question