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)
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)
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')