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

Why won't this if statement work properly?

Asked by
Manby7 32
4 years ago

I am trying to make a script so that when you click on the button, you can pay with materials to build the house.

game.Players.PlayerAdded:Connect(function(player)
    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local remotes = ReplicatedStorage:WaitForChild("Remotes")

    local leaderstats = player:WaitForChild("leaderstats")

    local Inventory = player:WaitForChild("Inventory")


    game.ReplicatedStorage.Remotes.BuyHouse.OnServerEvent:Connect(function()
        if leaderstats.Wood.Value >= 50 then
            leaderstats.Wood.Value = player.leaderstats.Wood.Value - 50
            Inventory.BoughtHouse.Value = true
            ReplicatedStorage.Text.Value = "-50 Wood"
            print("Enough coins")
        elseif leaderstats.Wood.Value <= 50 then
            ReplicatedStorage.Text.Value = "Not Enough Materials!"
            wait(2)
            print("Not enough coins!")
            ReplicatedStorage.Text.Value = "Press E to open the house upgrade menu!"
        end
    end)

end)

For some reason, no matter how much wood I have, it always says "Not Enough Materials".

Any help would be appreciated!

1
Try to print leaderstats.Wood.Value and see what it gives. Soban06 410 — 4y
0
Yeah there is nothing wrong with the statement, make sure you are setting Wood.Value on the server as setting it on the client won't replicate it. imKirda 4491 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

My conclusion on your issue is that you're possibly modifying the wood value on the client.

Remember this:

FilteringEnabled disallows the client from replicating most changes to the server. If you modify a value on the client, the change will not be seen on the server. If you desire client-server communication, use RemoteEvents or RemoteFunctions.

I also noticed you have an error in your if statement:

elseif leaderstats.Wood.Value <= 50 then

The original condition already checks to see if the value is equal to 50:

if leaderstats.Wood.Value >= 50 then

You should replace your elseif with just else:

if leaderstats.Wood.Value >= 50 then
    -- First code segment
else
    -- Second code segment

else will supply the following code in the event that no other conditions pass. Since x >= 50 checks if x is either greater than or equal to 50, there's no real reason to use an elseif because it's a waste of typing when compared to using else in this scenario.

0
Wow! what a blunder on my part. To test the game, I gave the wood to myself on the client, forgetting that it wouldn't be replicated to the server! Manby7 32 — 3y
Ad
Log in to vote
0
Answered by 4 years ago

What happens is that on the elseif statement, you say if it equals the same or less, not only less, and with the if statement you're saying it equals the same or more, to fix it, just do

elseif leaderstats.Wood.Value < 50 then

which gives us

game.Players.PlayerAdded:Connect(function(player)
    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local remotes = ReplicatedStorage:WaitForChild("Remotes")

    local leaderstats = player:WaitForChild("leaderstats")

    local Inventory = player:WaitForChild("Inventory")


    game.ReplicatedStorage.Remotes.BuyHouse.OnServerEvent:Connect(function()
        if leaderstats.Wood.Value >= 50 then
            leaderstats.Wood.Value = player.leaderstats.Wood.Value - 50
            Inventory.BoughtHouse.Value = true
            ReplicatedStorage.Text.Value = "-50 Wood"
            print("Enough coins")
        elseif leaderstats.Wood.Value < 50 then
            ReplicatedStorage.Text.Value = "Not Enough Materials!"
            wait(2)
            print("Not enough coins!")
            ReplicatedStorage.Text.Value = "Press E to open the house upgrade menu!"
        end
    end)

end)
0
That won't change anything. He is likely modifying the stat locally; the Server is recognizing the Value's original state, 0, due to denied replication. Ziffixture 6913 — 3y
0
You also don't need to affirm whether the Value is beneath 50 as the conditional of >= counterpart in an else clause alone already refers to such. Ziffixture 6913 — 3y

Answer this question