So i made this door buy gui when the player click “yes” the door opens, but he can buy this door withou having the money and the value goes to negative, how can I fix this?
The value is not controlled by an if statement. All you need to do is add an if
statement and subtract the value if they have sufficient amount and that it isn’t bought already.
On another note, this is flawed if you’re planning on changing values on the client instead of the server. For extra security, use remotes and “communicate” the client to the server to do something while server is running the calculations.
You have to use a if statement to check if he/she has the money and then a else statement so it can function
Exactly like @anon81993163 mentioned, adding an if Statement will definitely solve the problem here. Since you’re using a script that is Local from my knowledge (since most GUI scripts are anyways so I’m assuming that it is)
You could check for the leaderstat value by referencing the local player
LPlayer = game.Players.LocalPlayer
Then right after that you can plug the reference with the actual if statement. However this solely depends on what your leaderstat is called, so for your Bobux value (by the looks of it) try this. Although I suck at scripting, I think I got this in the bag
LPlayer = game.Players.LocalPlayer
if LPlayer.leaderstats.Bobux.Value < 60 then -- Change "60" to the price of your door, since you mentioned below:
This portion* of your script will go at the top of your script, right under your function.
Edit: Don’t forget the end
for the if statement!
If I did this wrong, or it errors, I apologize, but I’ve tried with my minimal scripting capabilities, and others may be able to assist further.
Have a wonderful day!
-Void_Xiety
I just tested my Script, and it works perfectly fine with a TextButton
inside of a GUI, frame, or whatever. If you’re curious what I wrote for that button, was a simple little test script. Hope this helps OP!
LPlayer = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if LPlayer.leaderstats.Bobux.Value < 60 then
script.Parent.Text = "You don't have enough Bobux. Get more, then try again."
wait(3)
script.Parent.Text = "Click" -- Original name of the button
elseif LPlayer.leaderstats.Bobux.Value >= 60 then
script.Parent.Text = "Purchase completed."
print("You have enough money. Purchase completed.")
end
end)
Also I believe it’s possible to change part transparency through LocalScripts, and pretty much every property on parts in the Workspace. However, others will not see anything change, only the Client who used the script (the script inside the button)
I am just adding on to what @Vyntrick said: you will want to make sure your script is checking that the player’s amount of “Bobux” is less than (or greater than) or equal to the price of the door. @Vyntrick’s script just checks for less or greater than. You also don’t need two if statements, you’ll just need one with an else added to the end of it:
if LPlayer.leaderstats.Bobux.Value >= 60 then --replace 60 with the price of the door
--Code that runs if the player can afford the item
else
--Code that runs if the player can't afford the item
end
It actually doesn’t check for only less than. If you looked at the script you can clearly see that it also has greater than (60, or above).
This is just an example that I wrote up quickly. It still works the same way. Sure, two if’s are not needed, but it doesn’t really affect the way the script runs.
Sorry, I accidentally forgot to add that. What I meant was that your script does work, it just doesn’t do anything if the Bobux amount is equal to 60.
Oh yeah, I can easily fix that though. I believe I need to replace >
with >=
or something like that from what I’ve seen from other scripts… Still sort of a beginner to this, and don’t really know all the signs for the math on this platform. I’ll look over the documentation.
Yeah, that is exactly how you would fix it. I’m just trying to help everyone
Another thing I forgot to mention: I am pretty sure that the local script will not change the Bobux value on the server side. This means that the player’s amount of Bobux will only go down for the player. That will mean that the player is technically not spending the Bobux. That being said, I am not 100% certain. If what I said is true, an example way to fix it would be to add a RemoteEvent to ReplicatedStorage (or Workspace). A server script would need to be added to the Workspace or ServerScriptService. Your local script (the script you already have) would have a line added:
game.ReplicatedStorage.RemoteEvent:FireServer(LPlayer, LPlayer.leaderstats.Bobux.Value) --RemoteEvent name and location may vary
Your server script will be:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Amount)
Player.leaderstats.Bobux.Value = Amount
end)
This will set the Bobux amount to be the same on both the server and the client side. Like I have already said, this may not be needed.
Local Scripts indeed CANNOT affiliate with the server in anyway, unless you fire it. So yes, this would be necessary if @quebrado were to be using a Local Script for this.
edit: your post also made me understand remote events a little more. Thanks