Menu button problem

Hello guys. I’ll say right away that the problem is serious. I can’t solve it.
For some reason, exchanging energy for money does not work. Can you find the reason? Even the cancel button doesn’t work. Please watch the video carefully. Option1 Option2 Option3 It doesn’t interact.


scripts Mаin


local Character = nil

game.ReplicatedStorage.Merchant1.Event:Connect(function(item, data, character)
Character = character

script.Parent.Option1.Visible = false
script.Parent.Option2.Visible = false
script.Parent.Option3.Visible = false


script.Parent.Dialogue.Text = "Do you want to exchange energy for money?"
script.Parent.Option1.Text = "Exchange all energy"
script.Parent.Option2.Text = "Exchange 50% energy"
script.Parent.Option3.Text = "No, thanks"

script.Parent.Option1.Visible = true
script.Parent.Option2.Visible = true
script.Parent.Option3.Visible = true

end)

script.Parent.Option1.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Character)
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = energyValue

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = 0
end

script.Parent.Parent:Destroy()

end)

script.Parent.Option2.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Character)
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = math.floor(energyValue / 2)

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = energyValue - exchangedEnergy
end

script.Parent.Parent:Destroy()

end)


Decline


local Talked = false

function Talk(Label, Text)
for i = 1, #Text do
Label.Text = string.sub(Text, 1, i)
wait()
end
end

function CreateOptions(Option1Text, Option2Text, Option3Text)
script.Parent.Option1.Visible = true
script.Parent.Option1.Text = Option1Text

script.Parent.Option2.Visible = true
script.Parent.Option2.Text = Option2Text

script.Parent.Option3.Visible = true
script.Parent.Option3.Text = Option3Text

end

while wait() do
if not Talked and not script.Disabled then
Talked = true
Talk(script.Parent.Dialogue, “Do you want to exchange energy for money?”, “Exchange all energy”, “Exchange 50% energy”,“No,Thx”)
CreateOptions(“Exchange all energy”, “Exchange 50% energy”, “No,Thx”)
end
end

script.Parent.Option1.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

script.Parent.Option2.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

script.Parent.Option3.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

2 Likes


The while loop cancels out the rest of the code, “break” inside the while loop would make it stop. An alternative way would be to move the code above the while loop.


Like this?

Decline

local Talked = false

function Talk(Label, Text)
for i = 1, #Text do
Label.Text = string.sub(Text, 1, i)
wait()
end
end

function CreateOptions(Option1Text, Option2Text, Option3Text)
script.Parent.Option1.Visible = true
script.Parent.Option1.Text = Option1Text

script.Parent.Option2.Visible = true
script.Parent.Option2.Text = Option2Text

script.Parent.Option3.Visible = true
script.Parent.Option3.Text = Option3Text
end

script.Parent.Option1.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

script.Parent.Option2.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

script.Parent.Option3.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

while wait() do
if not Talked and not script.Disabled then
Talked = true
Talk(script.Parent.Dialogue, “Do you want to exchange energy for money?”, “Exchange all energy”, “Exchange 50% energy”,“No,Thx”)
CreateOptions(“Exchange all energy”, “Exchange 50% energy”, “No,Thx”)
end
end
1 Like

It works thanks, but this part of the script does not work. Mаin


local Character = nil

game.ReplicatedStorage.Merchant1.Event:Connect(function(item, data, character)
Character = character

script.Parent.Option1.Visible = false
script.Parent.Option2.Visible = false
script.Parent.Option3.Visible = false


script.Parent.Dialogue.Text = "Do you want to exchange energy for money?"
script.Parent.Option1.Text = "Exchange all energy"
script.Parent.Option2.Text = "Exchange 50% energy"
script.Parent.Option3.Text = "No, thanks"

script.Parent.Option1.Visible = true
script.Parent.Option2.Visible = true
script.Parent.Option3.Visible = true

end)

script.Parent.Option1.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Character)
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = energyValue

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = 0
end

script.Parent.Parent:Destroy()

end)

script.Parent.Option2.MouseButton1Click:Connect(function()
local player = game.Players:GetPlayerFromCharacter(Character)
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = math.floor(energyValue / 2)

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = energyValue - exchangedEnergy
end

script.Parent.Parent:Destroy()

end)

Can you show where you fire the Merchant1 Event?

This is the logic. Press Option1. + Money -Energy. Option2 + 50%money - energy%50


I don’t know this is the only place where it is used.

try this: Decline

local Talked = false
local player = script.Parent.Parent.Parent.Parent
if not player:IsA("Player") then return end

function Talk(Label, Text)
for i = 1, #Text do
Label.Text = string.sub(Text, 1, i)
wait()
end
end

function CreateOptions(Option1Text, Option2Text, Option3Text)
script.Parent.Option1.Visible = true
script.Parent.Option1.Text = Option1Text

script.Parent.Option2.Visible = true
script.Parent.Option2.Text = Option2Text

script.Parent.Option3.Visible = true
script.Parent.Option3.Text = Option3Text
end

script.Parent.Option1.MouseButton1Click:Connect(function()
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = energyValue

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = 0
end
script.Parent.Parent:Destroy()
end)

script.Parent.Option2.MouseButton1Click:Connect(function()
if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = math.floor(energyValue / 2)

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = energyValue - exchangedEnergy
end
script.Parent.Parent:Destroy()
end)

script.Parent.Option3.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

while wait() do
if not Talked and not script.Disabled then
Talked = true
Talk(script.Parent.Dialogue, “Do you want to exchange energy for money?”, “Exchange all energy”, “Exchange 50% energy”,“No,Thx”)
CreateOptions(“Exchange all energy”, “Exchange 50% energy”, “No,Thx”)
end
end```
1 Like

But energyVаlue not globаl. what to do

The script works the same way the Merchant1 Event would, try it.

Bro, it works. Thank you very much! How can you insert scripts into such a table on the forum? like this

You can use three ``` at the beginning and the end of the line of code. Also no problem! Feel free to message me if there’s anything else.

Thank you! but wait a second. Players.MaaXGaz20.PlayerGui.MerchantGUI.Frame.Decline:37: attempt to compare number < nil when I click 50% exchange

yep my bad, replace decline with this

local Talked = false
local player = script.Parent.Parent.Parent.Parent
if not player:IsA("Player") then return end

function Talk(Label, Text)
for i = 1, #Text do
Label.Text = string.sub(Text, 1, i)
wait()
end
end

function CreateOptions(Option1Text, Option2Text, Option3Text)
script.Parent.Option1.Visible = true
script.Parent.Option1.Text = Option1Text

script.Parent.Option2.Visible = true
script.Parent.Option2.Text = Option2Text

script.Parent.Option3.Visible = true
script.Parent.Option3.Text = Option3Text
end

script.Parent.Option1.MouseButton1Click:Connect(function()
local energyValue = player.leaderstats.Energy.Value

if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = energyValue

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = 0
end
script.Parent.Parent:Destroy()
end)

script.Parent.Option2.MouseButton1Click:Connect(function()
local energyValue = player.leaderstats.Energy.Value
if energyValue > 0 then
    local cashValue = player.leaderstats.Cash.Value
    local exchangedEnergy = math.floor(energyValue / 2)

    player.leaderstats.Cash.Value = cashValue + exchangedEnergy
    player.leaderstats.Energy.Value = energyValue - exchangedEnergy
end
script.Parent.Parent:Destroy()
end)

script.Parent.Option3.MouseButton1Click:Connect(function()
script.Parent.Parent:Destroy()
end)

while wait() do
if not Talked and not script.Disabled then
Talked = true
Talk(script.Parent.Dialogue, “Do you want to exchange energy for money?”, “Exchange all energy”, “Exchange 50% energy”,“No,Thx”)
CreateOptions(“Exchange all energy”, “Exchange 50% energy”, “No,Thx”)
end
end
1 Like

Thanks again. Now everything works as it should.

thx

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.