Does anybody know how to trigger a function call after tweening? I want to show some text after the tween stops.
Tweens come with a Completed event, and you can attach a callback to it.
In a LocalScript, you can have something like this :
local TweenService = game:GetService("TweenService")
-- find your GUI element to animate
local lp = game.Players.LocalPlayer
local exampleScreen = Instance.new("ScreenGui", lp.PlayerGui)
exampleScreen.Name = "Example"
local label = Instance.new("TextLabel")
label.Position = UDim2.new(0,0,0,0)
label.Size = UDim2.new(0, 10, 0, 10)
label.Parent = exampleScreen
-- choose some properties to animate
local goal = {}
goal.Position = UDim2.new(0.5, -20, 0.5, -100)
goal.Size = UDim2.new(0.5, 40, 0.5, 200)
-- create the tween
local time = 5 --seconds
local tweenInfo = TweenInfo.new(time)
local tween = TweenService:Create(label, tweenInfo, goal)
-- listen for when the tween is finished
tween.Completed:Connect( function(tweenState)
label.Text = "Tween Completed"
end)
-- play the tween
tween:Play()
You have to set the tween time in tweeninfo variable. Just make it wait() for the same time as the tween takes to play() and then trigger a function.
Related
The script is very messy due to multiple attempts of trying to fix it from various places and people, yet it led to square one each time...
Local script:
local Cash500 = script.Parent
local A500 = script.Parent.A500
local player = game.Players.LocalPlayer ReplicatedStorage = game:GetService("ReplicatedStorage")
MPS = game:GetService("MarketplaceService")
id = 3736929511
local player = game.Players.LocalPlayer
local remoteEvent = ReplicatedStorage:WaitForChild("RE500")
--Enlarges size when mouse hovers
script.Parent.MouseEnter:Connect(function(x, y)
Cash500.Size = UDim2.new (0, 90, 0, 85)
A500.Size = UDim2.new(0, 75, 0, 30)
end)
--Resets size on not hovering mouse after mouse hovered
script.Parent.MouseLeave:Connect(function(x, y)
Cash500.Size = UDim2.new (0, 75, 0, 70)
A500.Size = UDim2.new(0, 60, 0, 15)
end)
--Purchase 500 money for 35 robux
--Fires remoteEvent when clicked- goes to RemoteEventController that gives money over
Cash500.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(player, id)
remoteEvent:FireServer("RE500")
end)
Leaderstat Handler:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money"
local Gems = Instance.new("IntValue", leaderstats)
Gems.Name = "Gems"
end)
local function addTime(player)
while true do
local RandomNumber = math.random(1,5)
wait(3)
player.leaderstats.Money.Value+=RandomNumber -- Same as Value=value+1 it is now- Value+=1
end
end
game.Players.PlayerAdded:Connect(addTime)
RemoteEvent Controller:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MPS = game:GetService("MarketplaceService")
local remoteEvent = ReplicatedStorage:WaitForChild("RE500")
local function Additional500(player)
print(player.Name .. " fired the remote event")
print("Updated! <3")
end
MPS.ProcessReceipt = function(receiptInfo)
print("OOOF")
if receiptInfo.ProductId == 3736929511 then
print("Start!")
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 500
print("Ended!")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
remoteEvent.OnServerEvent:Connect(Additional500)
There are no error messages coming out of the output but this when you click it comes up:
The Error message
Anyone know what to do ? Thanks! :)
I tried to make a code that if a RemoteEvent fire on Server, then the same RemoteEvent fires the client. When a local script receives the RemoteEvent FireClient then the Tween starts but this isn't occurring. The Frame don't turns Visible too. The output don't return any error. How i can fix that?
Code below:
local framground = script.Parent.Frame.GroundSmash.GroundSmash
local punch = script.Parent.Frame.EtoPunch.PunchRecharge
local TweenS = game:GetService("TweenService")
local TweenInf = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Tweenin = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Tween = TweenS:Create(framground, TweenInf, {Size = UDim2.new(0,1, 0, 67)})
local TweenC = TweenS:Create(punch, Tweenin, {Size = UDim2.new(0,1, 0, 67)})
game.ReplicatedStorage.CombatSkills.GroundSmash.OnClientEvent:Connect(function()
framground.Visible = true
Tween:Play()
task.wait(5)
framground.Size = UDim2.new(0, 238, 0, 67)
framground.Visible = false
end)
game.ReplicatedStorage.CombatSkills.Combat.OnClientEvent:Connect(function()
punch.Visible = true
TweenC:Play()
task.wait(.5)
punch.Size = UDim2.new(0, 238, 0, 67)
punch.Visible = false
end)
I've tried switching from Vector3 to CFrame and it still hasn't been working. The best I've gotten is the part appearing like 10 feat in the air, this is what I have so far.
local Dog = game:GetService("ReplicatedStorage"):WaitForChild("PetTesting")
local RunService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
local Petfollow = game:GetService("ReplicatedStorage"):WaitForChild("PetFollow")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
wait(1)
local dc = Dog:Clone()
dc.Name = "Pet"
dc.Parent = char
local offsetCframe = CFrame.new(0, 0, 0)
dc.CFrame = char:WaitForChild("HumanoidRootPart").CFrame + offsetCframe
dc.Anchored = true
dc.CanCollide = false
dc.Transparency = 0
end)
end)
You can use a bit of CFrame manipulation as described in https://developer.roblox.com/en-us/api-reference/datatype/CFrame. I am not sure if this is the right way of doing this, so please reply if this is incorrect.
local offset = Vector3.new(0, 0, 10)
local newCFrame = humanoidRootPart.CFrame * offset
I am trying to create a Maze Runner game. When making the doors to open and close i scripted it using ROBLOX's built in tween service. I run and i get "The function Create is not a member of "UnionOperation"" I have never heard of this error and couldn't find a solution. I am trying to do this on a Union part. I have no idea what to do. I need the tween to work as expected (tween the part a few spaces).
TweenService = game:GetService("TweenService")
Door = script.Parent.Door2
Door1 = Door:WaitForChild("Door1")
Door2 = Door:WaitForChild("Door2")
local TweenInformationIn = TweenInfo.new(
6,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local Door1Open = {CFrame = CFrame.new(1226.993, 131.187, -769.185)}
local Door2Open = {CFrame = CFrame.new(1226.993, 131.187, -814.271)}
local Door1Close = {CFrame = CFrame.new(1226.993, 131.187, -749.831)}
local Door2Close = {CFrame = CFrame.new(1226.993, 131.187, -834.331)}
local Tween1Open = TweenService.Create(Door1, TweenInformationIn, Door1Open)
local Tween2Open = TweenService.Create(Door2, TweenInformationIn,Door2Open)
local TweenClose = TweenService.Create(Door1, TweenInformationIn, Door1Close)
local Tween2Close = TweenService.Create(Door2,TweenInformationIn,Door2Close)
Tween1Open:Play()
Tween2Open:Play()
Replace TweenService.Create with TweenService:Create
TweenService:Create(Door1, TweenInformationIn, Door1Open)
is equivalent to
TweenService.Create(TweenService, Door1, TweenInformationIn, Door1Open)
whereas you called
TweenService.Create(Door1, TweenInformationIn, Door1Open)
So inside TweenService.Create things went south because Door1 was where TweenService should have been.
There is actually a code sample in the Robolox manual that shows how to use TweenService.
https://developer.roblox.com/api-reference/function/TweenService/Create
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = game.Workspace
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local tween = TweenService:Create(part, tweenInfo, {Position = Vector3.new(0, 30, 0)})
tween:Play()
wait(10)
tween:Cancel() -- cancel the animation after 10 seconds
This code creates a cannon and 3 balloons, the cannon should shoot out a bullet that'll destroy balloons, along with the words. DUring the process the cannon should rotate and when i release my finger from the screen it shoots. For some reason it doesn't respond, cannon not rotating nor any bullet is shot.
local score = 0
local scoreText
local scoreForLevelComplete
local background
local infoBar
local restartBtn
local cannon
local levelNum
local cannonCharge = {}
local shot = {}
local cannonBall
local impulse = 0
local balloons = {}
local cannonCharge = {}
local shot = {}
function scene:createScene(event)
local group = self.view
background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 230
background.y = 195
scoreText = display.newText( "0", 0, 0, native.systemFont, 32 )
scoreText:setFillColor( 0,0, 0 )
scoreText.x = 87
scoreText.y = 28
group:insert( scoreText )
questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)
infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
infoBar.x = 10
infoBar.y = 25
restartBtn = display.newImage ("restartBtn.png")
group:insert(restartBtn)
restartBtn.x = 470
restartBtn.y = 300
cannon = display.newImage ("cannon.png")
group:insert(cannon)
cannon.x = 10
cannon.y = 270
cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true
local balloon = display.newImage ('balloon_fat_red.png', 495, 125)
group:insert(balloon)
balloon = display.newImage ('balloon_fat_red.png', 495, 175)
group:insert(balloon)
balloon = display.newImage ('balloon_fat_red.png', 495, 225)
group:insert(balloon)
local balloonText1 = display.newText('\227\129\130', 495, 125)
balloonText1:setFillColor( 1,1, 0 )
local balloonText2 = display.newText('\227\129\132', 495, 170)
balloonText2:setFillColor( 1,1, 0 )
local balloonText3 = display.newText('\227\129\134', 495, 225)
balloonText3:setFillColor( 1,1, 0 )
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
group:insert(balloonText1)
group:insert(balloonText2)
group:insert(balloonText3)
function ballCollision(e)
if (e.other.name == 'balloon') then
scene.updateScore()
e.target:removeSelf()
print ('remove balloon text')
e.other:removeSelf()
audio.play(pop)
end
end
function cannonCharge:touch(e)
if(e.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
end
end
function charge()
local degreesPerFrame = 0.5
cannon.rotation = cannon.rotation - degreesPerFrame
impulse=impulse-0.2
if(cannon.rotation < -46) then
cannon.rotation = -46
impulse = -3.2
end
end
function shot:touch(e)
if(e.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = true
cannon.rotation = 0
cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
group:insert(cannonBall)
-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
end
This is my enterscene function
function scene:enterScene( event )
local group = self.view
background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot)
end
I know the Corona docs say that listener can be a table object when call addEventListener('event', listener) but I have never seen or used that. There is no advantage in posted code to have functions defined inside the createScene since they are global and you already have a bunch or module-local variables. Try pulling the listeners out and making them regular functions:
local canon
...
local cannonCharge = function(event)
if event.phase == 'began' then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
end
end
local shot = function(event)
...
end
local function charge()
...
end
... other local functions ...
function scene:createScene(event)
...
end
Also, confirm that your touch listeners are being called by printing something inside each one.
Finally, and most importantly, you only added the last balloon to the physics so the bullet can only collide with that one balloon. The same way that you had to add group:insert(balloon) after each balloon created, you should have physics.addBody(balloon, ...) after each group insert. So do this:
local balloon1 = display.newImage ('balloon_fat_red.png', 495, 125)
local balloon2 = display.newImage ('balloon_fat_red.png', 495, 175)
local balloon3 = display.newImage ('balloon_fat_red.png', 495, 225)
group:insert(balloon1)
group:insert(balloon2)
group:insert(balloon3)
physics.addBody(balloon1)
physics.addBody(balloon2)
physics.addBody(balloon3)
balloon1.bodyType = 'static'
balloon2.bodyType = 'static'
balloon3.bodyType = 'static'
table.insert(balloons, balloon1)
table.insert(balloons, balloon2)
table.insert(balloons, balloon3)
There is a lot of code duplication there, and adding more balloons requires many lines to change, so you might as well factor out the duplicate code into a function:
local function createBalloon(x, y)
local balloon = display.newImage ('balloon_fat_red.png', x, y)
group:insert(balloon)
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
end
createBalloon(495, 125)
createBalloon(495, 175)
createBalloon(495, 225)
which has the advantage that if you need more balloon you won't forget any settings, and any new settings put in createBallon so all balloons have same config (except for function parameters like x,y etc).
Update: Determine which balloon in collision handler
Depends why you need to know which of the balloons. For example if it's because balloon 1 gives 10 pts while 3 gives 30, then there are better ways: you can add your fields to objects, like you could have balloon1.points = 10, balloon2.points = 30 etc (you would make that a function argument of createBalloon) and in collision handler just use score = score + e.other.points. You should only need to use Local Collision Handling because only need to know when the cannon ball collides with balloons. To figure out if e.other is a balloon, easiest is to add a property when you create balloon:
local function createBalloon(x, y, balloonText)
local balloon = ...
...
balloon.isBalloon = true -- only balloon objects will have this
balloon.label = balloonText
end
Couple notes on the above: another custom property is label since you want to remove the balloon text in the collision handler, easiest is to have a property for it. But do not remove objects involved in collision in the collision handler, as explained in that document, use delayed removal. So your handler becomes
function ballCollision(e)
if e.other.isBalloon then
scene.updateScore(e.other.points)
timer.performWithDelay(1, e.target.removeSelf, e.target)
timer.performWithDelay(1, e.other.removeSelf, e.target)
e.other.label:removeSelf() -- this is ok: not involved in collision
audio.play(pop)
end
end
You have declared balloons as a array and using the balloon
as a variable while assigning images to them. So you need to declare 3 separate balloon object like balloon text or if you are using array then you need to declare like this.
for i=1,3 do
balloon[i] = display.newImage ('balloon_fat_red.png', 495, 225)
group:insert(balloon[i])
end
So it will identify which balloon you want to shoot.