Problem adding 2 different values

I can’t seem to get these 2 values to pair. I really don’t know what the problem is? Nothing comes up in the output.

Stats (values) Location:
image_2023-01-03_182513321

Part Touch Script:

script.Parent.Touched:connect(function(part)
	
	if part.Name == "Torso" then
	
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    local cash = plr.leaderstats.Cash.Value
	local woodvalue = plr.woods.Wood.Value
		
if woodvalue >0 then
cash = cash + woodvalue
woodvalue = 0
			
end
end
end)   

Try to format your code next time so the reading isn’t that difficult for us.
However connect is not a proper method of RbxScriptSignal type, instead use Connect.
Other problem is, that by using local cash = ...Value, you’re caching value of a primitive type, remember, that primitive values as numbers and strings are final, as well known as immutable or unchangeable.
If you want to set the value to these objects, you have to use plr.leaderstats.Cash.Value = ... again, and so on for other NumberValue objects.

script.Parent.Touched:Connect(function(part)

if part.Name == "Torso" then

   local plr = game.Players:GetPlayerFromCharacter(part.Parent)
   local cash = plr.leaderstats.Cash.Value
   local woodvalue = plr.woods.Wood.Value

   if woodvalue >0 then
      cash = cash + woodvalue
      woodvalue = 0
      plr.leaderstats.Cash.Value = cash -- Added line
      plr.woods.Wood.Value = woodvalue -- Added line
   end
end
end)

I’m not sure if this is right, but whenever I’ve used value instances I’ve never been able to reference the actual values of the instance in a variable if that makes any sense.

Try this instead:

script.Parent.Touched:connect(function(part)
	if part.Name == "Torso" then
       local plr = game.Players:GetPlayerFromCharacter(part.Parent)
       local cash = plr.leaderstats.Cash
       local woodvalue = plr.woods.Wood
		
      if woodvalue.Value > 0 then
         cash.Value += woodvalue.Value
         woodvalue.Value = 0
      end
   end
end)   
1 Like

The torso might not touch your part unless the character falls over. Instead, allow any part of a potential character to be detected. Like this:

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		local Cash = plr.leaderstats.Cash
		local Wood = plr.woods.Wood
		if Wood.Value > 0 then
			Cash.Value += Wood.Value
			Wood.Value = 0		
		end
	end
end)

I also fixed a few other things in your script.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.