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

Transferring a leaderstat value to another value not working? [ :OnTouch function ]

I am working on a project and when a person touches the trigger the script is supposed to transfer the "squeaks" - leaderstat value to the "coins" -leaderstat value. I have listed my script below:

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr then
        plr.leaderstats.Coins = plr.leaderstats.Coins.Value + plr.leaderstats.Sqeaks.Value
		plr.leaderstats.Sqeaks.Value = 0
	end
end)

2 comments

0

Does it show any errors in the output log?

0

Hey, did you put this script in a normal script and not a local script?

2 answers

✓ Accepted answer

You might need to check if the touch even had a player inside it in the first place. Also doing this I think would make the if plr useless so I just removed it. Also it says plr.leaderstats.Coins but no .value so I changed that also. So here:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

        plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + plr.leaderstats.Sqeaks.Value
        plr.leaderstats.Sqeaks.Value = 0

	end
    end
end)

2 comments

0

Thank you so much!

0

np :)

Oh there's a error. at line 5, you chnaged the coins, and not the coin's value. Change top this:

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + plr.leaderstats.Sqeaks.Value
        plr.leaderstats.Sqeaks.Value = 0
    end
end)

Also, make sure to put this in a script an not a local script!

Hope this helped!

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