Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I add two variables together?

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)

2 comments

0

remove the .Value part from the variable. Then when you add them, add the .Value there. This might work

0

I said to change it for lines 16 and 17, not 15, 15 should stay the same

2 answers

✓ Accepted answer

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!

4 comments

0

Oh we have the same explanation lmao

0

But yours is better 

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

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??

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.