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

How could I make the click of a TextButton trigger an "animation" made through a script?

So, I'm working on a game with a friend. I came across a problem when I was making a teleporter thing. I was going to make a teleporter GUI in which if you clicked "Confirm", the teleporter itself would have an "animation", or in other words hover, spin and then come back down. Now, I have the script for the animation, which is:

local model = script.Parent
local speed = 100 -- the speed it spins at, change this to how fast you want it to spin

local orig = script.Parent
local rotation = 0
local cf = orig:GetModelCFrame()

for i=1, 20 do
model:TranslateBy(Vector3.new(0, 0.05, 0))
wait()
end

wait (2)
local function ModelCFrame(Model,Frame)
	local search,MidFrame,list = nil,Model:GetModelCFrame(),{}
	search=function(a)
		for n,o in pairs(a:GetChildren())do
			if(o:IsA("BasePart"))then
				table.insert(list,o)
			end
			search(o)
		end
	end
	search(Model)
	for n,o in pairs(list)do 
		o.CFrame=Frame*MidFrame:toObjectSpace(o.CFrame)
	end 
end

local i = 0
while(wait()) and i < 151 do
	i = i + 1
	rotation = rotation+(speed/5)
	if(rotation>360)then rotation = 0 end
	ModelCFrame(orig,cf*CFrame.Angles(0,math.rad(rotation),0))
end

Now, this script works. I tested it and the animation works flawlessly. Great! Now, for the next part, I had to make this "animation" run after the "Confirm" button. So, I basically copied and pasted the above code into a script in the TextButton, put an OnClick function around it and connected the function to MouseButton1Down to the "Confirm" TextButton. It looked something like this:

function tele()
-- (THE CODE THAT I POSTED ABOVE)
end
script.Parent.MouseButton1Down:connect(tele)

Turns out that it didn't work. I tried and experimented with different changes. Nothing. Then, on a whim, I tried to use an if statement to do this (I had no idea what I was doing, so don't blame me if it's horrendously wrong D:) and also failed. It looked something like this:

if script.Parent.MouseButton1Down then
-- (THE CODE THAT I POSTED ABOVE)
end

That failed as well. So, at this point, my friend and I are at a dead end. We have no idea what to do now, and we would really appreciate if someone could help us. And in case you want to see the end result of our game, look up the users, UsedMail (my friend) and/or IronSpider671(me) on ROBLOX. Thanks!

2 comments

0

Is this in a Localscript?

0

No, it's just in a normal script.

1 answer

✓ Accepted answer
if script.Parent.MouseButton1Down then
-- (THE CODE THAT I POSTED ABOVE)
end

Remember, even though the above code makes sense to us Humanz, it does not make sense to the computer.

In an if-then statement, the stuff between the if and then is called a conditional. If the conditional equals true, the code runs, otherwise, it does not.

if true then
	--always runs this code
end

if false then
	--never runs this code
end

if IronSpider671.AwesomenessLevel > 9000 then
	--The computer turns "IronSpider671.AwesomenessLevel " into either true or false. If it is true, this code runs
	print("WHAT!? 9000!?")
end

The reason why your code doesn't work, is because when you tested it out, it was parented differently than it was when you tried to run it in the TextButton.

In other words, when you tested it, script.Parent = teleporter. When you copy and pasted, script.Parent = TextButton, and your script tried to animate the TextButton instead of the teleporter.

I removed "model" from the beginning of your script, since it is the same thing as orig.

Here is your script, I didn't look through it really, but I made it work I think:

function tele()
local teleporterName = "Teleporter" --Make sure this is the only object in Workspace with this name
local speed = 100 -- the speed it spins at, change this to how fast you want it to spin

local orig = workspace:FindFirstChild(teleporterName,true)
local rotation = 0
local cf = orig:GetModelCFrame()

for i=1, 20 do
orig:TranslateBy(Vector3.new(0, 0.05, 0))
wait()
end

wait (2)

local function ModelCFrame(Model,Frame)
    local search,MidFrame,list = nil,Model:GetModelCFrame(),{}
    search=function(a)
        for n,o in pairs(a:GetChildren())do
            if(o:IsA("BasePart"))then
                table.insert(list,o)
            end
            search(o)
        end
    end
    search(Model)
    for n,o in pairs(list)do 
        o.CFrame=Frame * MidFrame:toObjectSpace(o.CFrame)
    end 
end

local i = 0
while(wait()) and i < 151 do
    i = i + 1
    rotation = rotation + (speed / 5)
    if (rotation > 360) then rotation = 0 end
    ModelCFrame(orig,cf*CFrame.Angles(0, math.rad(rotation), 0))
end

end
script.Parent.MouseButton1Down:connect(tele)

2 comments

0

Wow! Thanks, Validark! Really appreciate it! I'll tell my friend about this. He'll be delighted. Thanks again! You, sir, have an awesome day!

0

You too! Make sure to hit "Accept Answer" if my answer was what you were looking for.

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