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

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

Asked by 5 years ago

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)
0
Does it show any errors in the output log? jordysboy 53 — 5y
0
Hey, did you put this script in a normal script and not a local script? TheRealPotatoChips 793 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
Thank you so much! XxWingKing 96 — 5y
0
np :) CosmicIsGod 139 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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!

Answer this question