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

How do I activate a script when something is clicked?

Ok I put this script inside of a frame that has TextButtons for children, but I also have a LocalScript inside of the frame directly below the script and I want the LocalScript to Activate after the player has chosen classes, what's wrong with this?

player = script.Parent.Parent.Parent
backpack = player.Backpack

function chooseClass(class)
	for i, v in pairs(backpack:GetChildren()) do v:remove() end
	for i, v in pairs(class:GetChildren()) do
		if v:IsA("Tool") then
			v:clone().Parent = backpack
		elseif v:IsA("HopperBin") then
			v:clone().Parent = backpack
		end
	end
	
	script.Parent.Main.Visible = false
	script.Parent.Title.Visible = false
end

function onHumanoidDied(humanoid, player)
	script.Parent.Main.Visible = true
	script.Parent.Title.Visible = true
	end

for i, v in pairs(script.Parent.Main:GetChildren()) do
	v.MouseButton1Up:connect(function () chooseClass(v) end)
end

game.StarterGui.Class.Main.LocalScript.Disabled = true

function LocalScript()
	if LocalScript.Disabled == true then
		game.StarterGui.Class.Main.TextButton.MouseButton1Clicked:wait()
		game.StarterGui.Class.Main.LocalScript.Disabled = false
	end
end

LocalScript.TextButton.MouseButton1Clicked:connect()

1 comment

0

Btw, Main is actually the Frame that's inside of the Screen GUI which is called class.

1 answer

✓ Accepted answer

The problem is two-fold.

First of all, if that is all of your code, that function never runs because it is never called.

Second, onClicked is not a valid member of anything, really.

Since you only want this code to run once (I assume), try using the wait method of MouseButton1Clicked, which is a member of TextButton:

game.StarterGui.Class.Main.LocalScript.Disabled = true

game.StarterGui.Class.Main.TextButton.MouseButton1Clicked:wait()
game.StarterGui.Class.Main.LocalScript.Disabled = false

Lastly, and this is just a readability thing, your placement of then is weird.

The 'standard' is to type it like this:

if condition then
	--[[code]]
end

--or

if condition then --[[code]] end

Instead of:

if condition
	then --[[code]]
end

This makes it easier to read the statement, especially if there are multiple 'code' lines being run, and it also makes it blatantly obvious when an if-statements checks are continued on the next line, which isn't the case in your code.

9 comments

0

Thank you for your help! So My script was correct but I had Then in the wrong place and I needed to add MouseButton1Clicked right?

0

No. Compare your code to mine. Like I said, you never called the Disabled function, and you never connected the MouseButton1ClickedEvent, or wait'd for it.

0

Mm, I'm a newbie to scripting this is actually my first script from scratch so I'll try to make this work.

0

And sorry to bug you but what does condition mean?

0

A condition is a requirement of some kind, as in the conditions of a contract. In programming, a conditional statement is one that evaluates to either 'true' or 'false'. In Lua specifically, 'true' is anything that isn't explcititly 'nil' or 'false', so things like: "if script.Parent then" work. The 'and' and 'or' keywords are primarily used in conditional statements.

0

Ok, I'm gonna post an answer of my new script I put together and I need you to tell me if it's right Ok?

0

Just edit your original question with the new code.

0

Mmk,

0

Yes, yes, I know this is not a requesting site but can you give me a script to do this? I'm gonna try to read it and understand it my main goal in scripting is to script like ChadtheCreator, I just LOVE animations! And the Keydown thing, I also like Hotkeys instead of numbers, almost like OnlineAdventure, you spam Q to fight.

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