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

Change script variable 'Gold' to 'KOs'?

Asked by 9 years ago

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)
0
Just change line 8 into "local gold = stats:FindFirstChild('KOs')" since gold is just a variable it doesn't matter if you change it or not. EzraNehemiah_TF2 3552 — 9y
0
Zephy, you there are 2 answers, you can look at 2 of them. I wrote the 2nd one because alpha didn't explain it well. I also added some things that might help you in later games! EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

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.

0
Thanks it worked and yes I am learning. Zephyrnite 0 — 9y
0
Okay, that's good. If you didn't know what Global Variable is, just go into basic objects for Studio and type Value; all of those you see are Global Variables. At least I learned them to be called Global Variables. They're very handy! alphawolvess 1784 — 9y
0
You didn't explain it well enough, good answers could be like mine, bluetaslem's, and Perci1's, and many more. They all explain it thoroughly so they don't ask a similar question again. EzraNehemiah_TF2 3552 — 9y
View all comments (2 more)
0
@Alpha, it's just a typo. I kinda laughed at that. EzraNehemiah_TF2 3552 — 9y
0
@Alpha, I had too. Should I make it italic? EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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!

0
I hop you do too alphawolvess 1784 — 9y
0
I know its a typo and hah, you Bolded xD alphawolvess 1784 — 9y

Answer this question