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

Selling a leaderstat value for another leaderstat value?

Asked by
Gybron 19
9 years ago

So I be tryin' to sell 5 potatoes (Potato) for 5 experience points (XP) 'ere in this dialog. When I try it, it takes no potato nor gives any XP! What's the error? It must be in the script, since the routes outside it have been checked.

local dialog = script.Parent
dialog.DialogChoiceSelected:connect(function(player, choice)

    local stats = player:FindFirstChild('leaderstats')
    if not stats then return end 

    local Gold = stats:FindFirstChild('Potato')
    if not Gold then return end 
    if choice == script.Parent.DialogChoice.ChoiceA then
        if Gold.Value <= 5 then           
        stats:findFirstChild('XP')
        XP.Value = XP.Value + 5
        stats:findFirstChild('Potato')
        Potato.Value = Potato.Value - 5
        end
    end
    end)
1
Line 10, try greater than or equal to 5 M39a9am3R 3210 — 9y
0
no effect, but that was a good point. :S My head is about to explode, what is wrong with this Gybron 19 — 9y

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

M39a9am3R's suggestion is right. You should use >= 5 instead of <= 5 since it's "if they have at least five gold, then use those five gold to..."


Please note that using FindFirstChild is only necessary when you're actually doing error checking. That means your use of it in line 7 is correct; however, the later uses are unnecessary since you do not do error-prevention with the value.


You never define the value XP, which you need to!

local XP = stats.XP -- Replaces line 11

Similarly, you use Potato later but never define it.

Instead of defining it like we did with XP just now, we should just keep using the Gold variable instead (this will also make it a little easier to re-use this snippet should we want to exchange other things for XP)


All together it should look like this:

local dialog = script.Parent
dialog.DialogChoiceSelected:connect(function(player, choice)
    local stats = player:FindFirstChild('leaderstats')
    if not stats then return end 
    local Gold = stats:FindFirstChild('Potato')
    if not Gold then return end 
    if choice == script.Parent.DialogChoice.ChoiceA then
        if Gold.Value >= 5 then
            local XP = stats.XP;
            XP.Value = XP.Value + 5
            Gold.Value = Gold.Value - 5
        end
    end
end)
Ad
Log in to vote
0
Answered by 9 years ago

You didn't set the variable of potato. So replace line 13 with this code below. If this helped please upvote my rep.

Potato = stats:FindFirstChild('Potato')

Answer this question