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

When i set a value to true it looks like the next elsif statement is also firing. Why is that?

Asked by 5 years ago
Edited 5 years ago

So I have this script that will give you a hammer and set Hammer.Value to true etc. But when I set it to true the elseif statement will react because the hammer value is true which causes my code to not work properly. Could someone help me?

local Hammer = game.ReplicatedStorage.Tools.Hammer:Clone()
local Replicator = game.ReplicatedStorage.Replicators.Shop.Purchase_Hammer
local Audio_Ctr = game.ReplicatedStorage.Replicators.Misc.Audio_Controller
local Button = workspace.Shop.Hammer_Item.Purchase_Button
local Purchase_Sound = "Purchase_Sound"
local insufficient_Funds = "Err_Purchasing"

Replicator.OnServerEvent:Connect(function(player)

    local Currency = player:WaitForChild("Currency")
    local Bits = Currency.Bits
    local Own_Hammer = player.ToolStat.Hammer
    --<-->--
if Bits.Value >=10 and Own_Hammer.Value == false then
    Bits.Value = Bits.Value -10
    Hammer.Parent = player.Backpack
    Audio_Ctr:FireClient(player,Purchase_Sound)
    print("True")
    Own_Hammer.Value = true
elseif Bits.Value <=10 or Own_Hammer.Value == true then
    print("FALSE")
    Audio_Ctr:FireClient(player,insufficient_Funds)
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

I may be wrong but I believe it has to do with how you wrote the if and elseif statements. For example if Bits.Value is 10 because >= implies that if it's bits.Value is bigger or EQUALED to 10 then and so on. That's the same for the OTHER Bits.Value if statement, implying that if Bits.Value is smaller or EQUALED to 10. So either way both if statements will give you "true" therefore running the code. Also for the elseif statement make sure to change it to "and" instead of "or", so that it will absolutely work and the value must be both true and less than 10. Tell me if the code below doesn't work.

local Hammer = game.ReplicatedStorage.Tools.Hammer:Clone()
local Replicator = game.ReplicatedStorage.Replicators.Shop.Purchase_Hammer
local Audio_Ctr = game.ReplicatedStorage.Replicators.Misc.Audio_Controller
local Button = workspace.Shop.Hammer_Item.Purchase_Button
local Purchase_Sound = "Purchase_Sound"
local insufficient_Funds = "Err_Purchasing"

Replicator.OnServerEvent:Connect(function(player)

    local Currency = player:WaitForChild("Currency")
    local Bits = Currency.Bits
    local Own_Hammer = player.ToolStat.Hammer
    --<-->--
if Bits.Value >=10 and Own_Hammer.Value == false then
    Bits.Value = Bits.Value -10
    Hammer.Parent = player.Backpack
    Audio_Ctr:FireClient(player,Purchase_Sound)
    print("True")
    Own_Hammer.Value = true
elseif Bits.Value < 10 and Own_Hammer.Value == true then
    print("FALSE")
    Audio_Ctr:FireClient(player,insufficient_Funds)
    end
end)
0
Thank you! It worked, this saves me a lot of time beacuse i had this problem in the past and i never knew why it wasn't working. Unscriptablee 20 — 5y
Ad

Answer this question