Alright so I have a script that will basically allow some of my NPC's to hand out items if a certain dialogue option is picked. What I need help is for the fact that items will cost Knockouts, not Gold. Script was taken from the ROBLOX Wiki under the NPC dialogue shop tutorial. How do I change the script so it tells the NPC to sell weapons based on KO's not Gold?
local dialog = script.Parent dialog.DialogChoiceSelected:connect(function(player, choice) -- Check the player has a stats object local stats = player:FindFirstChild('leaderstats') if not stats then return end -- And that the stats object contains a gold member local gold = stats:FindFirstChild('Gold') if not gold then return end if choice == script.Parent.DialogChoice.ChoiceA then if gold.Value >= 5 then -- 5 is the amount of gold you need to purchase this weapon game.ReplicatedStorage.Weapon1:Clone().Parent = player.Backpack gold.Value = gold.Value - 5 -- subtract the amount of gold you need to purchase end elseif choice == dialog.DialogChoice.ChoiceB then if gold.Value >= 10 then game.ReplicatedStorage.Weapon2:Clone().Parent = player.Backpack gold.Value = gold.Value - 10 end elseif choice == dialog.DialogChoice.ChoiceC then if gold.Value >= 15 then game.ReplicatedStorage.Weapon3:Clone().Parent = player.Backpack gold.Value = gold.Value - 15 end end end)
Now, my answer is based off of you having a Global Value called KOs to make your leaderboard. I also really hope you understand that script pretty well and that you're learning; not getting a free pass. Also, I will make it so you can lose KOs as you buy.
local dialog = script.Parent dialog.DialogChoiceSelected:connect(function(player, choice) -- Check the player has a stats object local stats = player:FindFirstChild('leaderstats') if not stats then return end -- And that the stats object contains a KOs member local KOs = stats:FindFirstChild('KOs') if not KOs then return end if choice == script.Parent.DialogChoice.ChoiceA then if KOs.Value >= 5 then -- 5 is the amount of KOs you need to purchase this weapon game.ReplicatedStorage.Weapon1:Clone().Parent = player.Backpack KOs.Value = KOs.Value - 5 -- subtract the amount of KOs you need to purchase end elseif choice == dialog.DialogChoice.ChoiceB then if KOs.Value >= 10 then game.ReplicatedStorage.Weapon2:Clone().Parent = player.Backpack KOs.Value = KOs.Value - 10 end elseif choice == dialog.DialogChoice.ChoiceC then if KOs.Value >= 15 then game.ReplicatedStorage.Weapon3:Clone().Parent = player.Backpack KOs.Value = KOs.Value - 15 end end end)
Basically all Gold names were changed to KOs . Again, I do not know if you have the exact names I put; check to be sure.
Like how Alphawolvess said, this would be correct, but this would take away KOs. Just replace all the "-15" with 0 or just remove that whole line.
local dialog = script.Parent dialog.DialogChoiceSelected:connect(function(player, choice) -- Check the player has a stats object local stats = player:FindFirstChild('leaderstats') if not stats then return end -- And that the stats object contains a KOs member local KOs = stats:FindFirstChild('KOs') if not KOs then return end if choice == script.Parent.DialogChoice.ChoiceA then if KOs.Value >= 5 then -- 5 is the amount of KOs you need to purchase this weapon game.ReplicatedStorage.Weapon1:Clone().Parent = player.Backpack end elseif choice == dialog.DialogChoice.ChoiceB then if KOs.Value >= 10 then game.ReplicatedStorage.Weapon2:Clone().Parent = player.Backpack end elseif choice == dialog.DialogChoice.ChoiceC then if KOs.Value >= 15 then game.ReplicatedStorage.Weapon3:Clone().Parent = player.Backpack end end end)
I feel like alpha didn't explain it well enough. What is happening is gold, the variable, is actually "gold" in the leaderstats. Alpha went through the work of changing all the "Gold" into "KOs". You really don't need to do that because a variable could be anything from purplepinkunicorns
to Bobs_Cow
. As long as the variable is the KO this should work.
This does not have to do with the script above, this has to do with dialog though.
Another cool thing to do with dialog is Chat. Dialog is actually chat. Now in games maybe you would like to censor something. In ROBLOX if a GUi or a name of a model with a head and humanoid can't say bad words or they will be invisible. So we could do that with the chatted method. First we need to know how to make the text over the head or character.
game:GetService("Chat"):Chat(workspace.Player1, "Hi, my name is Player1!", "Blue") --[[The chat service will make a chat bubble for Player1 in workspace. It will say, "Hi, my name is Player1!" and it will be colored blue.]]
Chat bubbles can only be colored(coloured) in 3 colors: red,blue,and green. This is also: Enemy, Neutral, or Friend in dialog. So the chat function has 3 arguments. The first one is "Who is saying the chat?" the second is "What is the thing going to say?" and the third one will say "What color will it be?".
game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) game:GetService("Chat"):Chat(player.Character, msg, "Blue") --The character will say the message in blue. end) end)
We can also make the character decide what the color of the text is with string.sub
. Here is an example. If you say "R/" then it will be red. If you say "B/" it will be blue. If you say "G/" then it will be green.
game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(msg) if string.sub(msg,0,2) == "B/" then game:GetService("Chat"):Chat(player.Character, string.sub(msg, 3), "Blue") elseif string.sub(msg,0,2) == "R/" then game:GetService("Chat"):Chat(player.Character, string.sub(msg, 3), "Red") elseif string.sub(msg,0,2) == "G/" then game:GetService("Chat"):Chat(player.Character, string.sub(msg, 3), "Green") end end) end)
This is was so you understand this better. I hope you understand now!