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!
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.
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)