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?

01local Hammer = game.ReplicatedStorage.Tools.Hammer:Clone()
02local Replicator = game.ReplicatedStorage.Replicators.Shop.Purchase_Hammer
03local Audio_Ctr = game.ReplicatedStorage.Replicators.Misc.Audio_Controller
04local Button = workspace.Shop.Hammer_Item.Purchase_Button
05local Purchase_Sound = "Purchase_Sound"
06local insufficient_Funds = "Err_Purchasing"
07 
08Replicator.OnServerEvent:Connect(function(player)
09 
10    local Currency = player:WaitForChild("Currency")
11    local Bits = Currency.Bits
12    local Own_Hammer = player.ToolStat.Hammer
13    --<-->--
14if Bits.Value >=10 and Own_Hammer.Value == false then
15    Bits.Value = Bits.Value -10
View all 24 lines...

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.

01local Hammer = game.ReplicatedStorage.Tools.Hammer:Clone()
02local Replicator = game.ReplicatedStorage.Replicators.Shop.Purchase_Hammer
03local Audio_Ctr = game.ReplicatedStorage.Replicators.Misc.Audio_Controller
04local Button = workspace.Shop.Hammer_Item.Purchase_Button
05local Purchase_Sound = "Purchase_Sound"
06local insufficient_Funds = "Err_Purchasing"
07 
08Replicator.OnServerEvent:Connect(function(player)
09 
10    local Currency = player:WaitForChild("Currency")
11    local Bits = Currency.Bits
12    local Own_Hammer = player.ToolStat.Hammer
13    --<-->--
14if Bits.Value >=10 and Own_Hammer.Value == false then
15    Bits.Value = Bits.Value -10
View all 24 lines...
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