Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this script work?

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

4 comments

0

Is this a localscript?

0

Yes.

1

Is this the entire script? You don't ever connect onClicked to anything.

0

Oh... thanks, I'll play around with this.

1 answer

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 )

This discussion is preserved as it was. New answers, comments, and votes are closed.