I'm a newbie at scripting and I've been trying to make a code to buy items upon clicking a dialog and having the money for it. Here is the code for the dialog
local dialog = script.Parent.Parent local choice1 = dialog.DialogChoice.KatanaChoice local user = --how can I find the user that clicked the dialog here? local stats = user:findFirstChild("leaderstats") local bank = stats:findFirstChild("Bank") local cash = stats:findFirstChild("Cash") dialog.DialogChoiceSelected:connect(function(plr, choice) if choice == choice1 then if cash.Value >= 25000 or bank.Value >=25000 then local katana = game.Lighting.Katana:clone() katana.Parent = plr.Backpack local choice2 = dialog.DialogChoice.RocketChoice if choice == choice2 then if cash.Value >= 100000 or bank.Value >= 100000 then local Rocket = game.Lighting.RocketLauncher:clone() Rocket.Parent = plr.Backpack local choice3 = dialog.DialogChoice.SpinnerChoice if choice == choice3 then if cash.Value >=500000 or bank.Value >=500000 then local Spinner = game.Lighting.AutisticShuriken:clone() Spinner.Parent = plr.Backpack local choice4 = dialog.DialogChoice.PistolChoice if choice == choice4 then if cash.Value >=8000 or bank.Value >=8000 then local Pistol = game.Lighting.GLOCK18:clone() Pistol.Parent = plr.Backpack end end end end end end end end end )
And here is the code for the leaderboard (free model)
function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local c = Instance.new("IntValue") c.Name = "Cash" c.Value = 50000 c.Parent = stats stats.Parent = newPlayer local b = Instance.new("IntValue") b.Name = "Bank" b.Value = 10000 b.Parent = stats stats.Parent = newPlayer newPlayer.Changed:connect(function (property) if (property == "Character") then onPlayerRespawned(newPlayer) end end) end game.Players.ChildAdded:connect(onPlayerEntered)
Alright, so you're on the right track, with some minor modifications you will be able to achieve what you want!
DialogChoiceStarted returns two parameters, that you have already set variables for in your function:
dialog.DialogChoiceSelected:connect(function(plr, choice) -- plr here returns the player object of the player that clicked the dialog choice end)
Thus, we will be able to use the variable "plr" to check if the player has sufficient funds
So after the modifications, the script should look something like this:
local dialog = script.Parent.Parent local choice1 = "KatanaChoice" local choice2 = "RocketChoice" local choice3 = "SpinnerChoice" local choice4 = "PistolChoice" function deductMoney(stat, amount) stat.Value = stat.Value + amount end dialog.DialogChoiceSelected:connect(function(plr, choice) local stats = plr:findFirstChild("leaderstats") local bank = stats:findFirstChild("Bank") local cash = stats:findFirstChild("Cash") if choice.Name == choice1 then local price = 25000 -- set the price value to the price the item of the choice is if cash.Value >= price then deductMoney(cash, -price) -- take away the money from the cash stat local katana = game.Lighting.Katana:Clone() katana.Parent = plr.Backpack elseif bank.Value >= price then -- else if there is not enough money in cash stat deductMoney(bank, -price) -- take away the money from bank stat local katana = game.Lighting.Katana:Clone() katana.Parent = plr.Backpack else print("Not Enough Funds") end elseif choice.Name == choice2 then local price = 100000 if cash.Value >= price then deductMoney(cash, -price) local Rocket = game.Lighting.RocketLauncher:Clone() Rocket.Parent = plr.Backpack elseif bank.Value >= price then deductMoney(bank, -price) local Rocket = game.Lighting.RocketLauncher:Clone() Rocket.Parent = plr.Backpack else print("Not Enough Funds") end elseif choice.Name == choice3 then local price = 500000 if cash.Value >= price then deductMoney(cash, -price) local Spinner = game.Lighting.AutisticShuriken:Clone() Spinner.Parent = plr.Backpack elseif bank.Value >= price then deductMoney(bank, -price) local Spinner = game.Lighting.AutisticShuriken:Clone() Spinner.Parent = plr.Backpack else print("Not Enough Funds") end elseif choice.Name == choice4 then local price = 8000 if cash.Value >= price then deductMoney(cash, -price) local Pistol = game.Lighting.GLOCK18:Clone() Pistol.Parent = plr.Backpack elseif bank.Value >= price then deductMoney(bank, -price) local Pistol = game.Lighting.GLOCK18:Clone() Pistol.Parent = plr.Backpack else print("Not Enough Funds") end end end)
Note: Storing objects in lighting is not the most efficient way of doing so, you can use ReplicatedStorage (if it has to be accessed by local scripts) or ServerStorage (can only be accessed by server scripts). I left it the same you way had it though, set to lighting.
Strong suggestion: when asking questions on here, please ensure correct tabbing of the code, it makes reading the code much easier.
Hope this helped!