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

Why doesn't this script work?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I'm trying to make this script work, to take away 100 "money", and make the textbox say "confirmed purchase", if they have more or equal, but if they have less, it says "Error!", and doesn't do anything. It dosen't work, and I need help to fix it. I cant find anything wrong, and nothing shows up in output.

Code :

local player = game.Players.LocalPlayer

function onClicked()
    if player:FindFirstChild("Money").Value >= 100 then ---------seeing if the money is greater than or equal to 100
        script.Parent.Text = "Confirmed purchase!" ------ telling the money has been confirmed
        player:FindFirstChild("Money").Value = player:FindFirstChild("Money").Value-100 ---- subtracting 100
    if player:FindFirstChild("Money").Value < 100 then ------- if the money is less than 100
        script.Parent.Text = "Error!" ------- saying error because not enough money
        wait(3) ---- waiting so the player can see it
        return ---- returning the script to the way it was before
    end
    end
end
0
Is this a localscript? Necrorave 560 — 9y
0
Yes. KennySfromTitan 106 — 9y
1
Is this the entire script? You don't ever connect `onClicked` to anything. BlueTaslem 18071 — 9y
0
Oh... thanks, I'll play around with this. KennySfromTitan 106 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

When you define a function, like onClicked, that function just sits there. It won't happen until you ask it to run. Since you never use the word onClicked in your script, except to define it -- so it's never going to happen.

Connecting to Events

ROBLOX frequently uses something called events. An event is an object that describes some thing that will happen over and over. You can connect a function to an event. What that means is whenever the event happens, the function will be called.

In your example, if script.Parent.Parent.Button is the button you want to press, script.Parent.Parent.Button.MouseButton1Down is an event for when you click on it.

You have to :connect to it, and give it the function you want to use (which in your case you called onClicked:

script.Parent.Parent.Button.MouseButton1Down:connect( onClicked )
Ad

Answer this question