You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I’m trying to remove money from the player’s IntValue named “Cash” and then add 1 to the IntValue named “Armor” if the player has enough money.
What is the issue?
In the console, it returns “Cannot compare string <= Number” and the rest of the script doesn’t run.
What solutions have you tried so far?
I’ve tried changing the spots, the cash value, how much money is removed, etc. but does not work.
local event = game.ReplicatedStorage.UIElements.MoneyRemoved
event.OnServerEvent:Connect(function(player, item, cost)
local cash = player.PlayerStats.Cash.Value
if cash >= cost then
game.ReplicatedStorage.UIElements.MoneyRemoved:FireClient(player, cost)
player.PlayerStats[item].Value = player.PlayerStats[item].Value + 1
player.PlayerStats.Cash.Value = player.PlayerStats.Cash.Value - cost
else
game.ReplicatedStorage.UIElements.NotEnoughMoney:FireClient(player)
end
end)
R_obotz
(Robotz)
May 7, 2022, 5:46pm
#2
Make sure the Cash value in PlayerStats folder is of ClassName “IntValue”
I suggest you print both cash and cost variables before the cash >= cost
condition to make sure they’re both numbers.
Try what @R_obotz said but also show us the part of your other script that fires the event
Well now it just says trying to compare nil <= number.
Here are both updated scripts:
Server Script
local event = game.ReplicatedStorage.UIElements.MoneyRemoved
event.OnServerEvent:Connect(function(player, cost)
local cash = player.PlayerStats.Cash.Value
print(cash)
print(cost)
if cash >= cost then
game.ReplicatedStorage.UIElements.MoneyRemoved:FireClient(player, cost)
player.PlayerStats.Armor.Value = player.PlayerStats.Armor.Value + 1
player.PlayerStats.Cash.Value = player.PlayerStats.Cash.Value - cost
else
game.ReplicatedStorage.UIElements.NotEnoughMoney:FireClient(player)
end
end)
Local Script
local event = game.ReplicatedStorage.UIElements.MoneyRemoved
local cost = script.Parent.Price.Value
script.Parent.MouseButton1Click:Connect(function(player)
event:FireServer(player, cost)
end)
It seems as if the cost value is the problem since it prints the correct cash value in the console but the Cost value is printed as “nil”
The problem is you don’t pass in a player to the FireServer() that happens automatically, so your server script is fine but your local script should be:
local event = game.ReplicatedStorage.UIElements.MoneyRemoved
local cost = script.Parent.Price.Value
script.Parent.MouseButton1Click:Connect(function(player)
event:FireServer(cost)
end)
BTW: You do however have to pass a player to FireClient
1 Like
Thank you!
I didn’t know you didn’t have to pass the player through a RemoteEvent.
It works now!
1 Like