Just as the title says. I need to save or store one of my tweens so that I can access it from inside another script. I have no idea where to start. Is something like this even possible?
It’s unclear what you are trying to achieve. But in general you share functions, objects, values, etc through ModuleScripts.
After trying what you said I realized that’s not going to fix my problem. In short I have a script when fired it creates a tween. When I fire it a second time I want the tween to stop. But because I fired it again it created a new tween which already overwrote the first tween. For some reason I can’t stop the first tween no matter what I do.
I’m not sure what you mean that you are firing scripts. I assume you mean firing event functions?
Anyway you can cancel a Tween
-- basic implementation
local tween = Tween:Create()
button.MouseButton1Click:Connect(function()
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
else
tween:Play()
end
end)
I meant remote events. You didn’t have to write that out as I already know how to stop a Tween. Let me give you a piece of my script.
local number = 0
game.ReplicatedStorage.EquipPet.OnServerEvent:Connect(function(player)
number = number + 1
for i, attachmentInHumRP in pairs(player.Character.HumanoidRootPart:GetChildren()) do
if attachmentInHumRP.Name == "Attachment1" then
local petAttachment = attachmentInHumRP
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1)
local upTween = TweenService:Create(petAttachment, tweenInfo, {
Position = Vector3.new(petAttachment.Position.X, 1.5, petAttachment.Position.Z),
Orientation = Vector3.new(petAttachment.Orientation.X, petAttachment.Orientation.Y, -5)
})
local downTween = TweenService:Create(petAttachment, tweenInfo, {
Position = Vector3.new(petAttachment.Position.X, 0, petAttachment.Position.Z),
Orientation = Vector3.new(petAttachment.Orientation.X, petAttachment.Orientation.Y, 5)
})
if number == 2 then
upTween:Cancel()
downTween:Cancel() --these doesn't stop the tween
wait(1)
attachmentInHumRP.Position = Vector3.new(-3.152,1.5,6.566)
attachmentInHumRP.Orientation = Vector3.new(0,-115,0)
end
upTween.Completed:Connect(function()
downTween:Play()
end)
downTween.Completed:Connect(function()
upTween:Play()
end)
upTween:Play()
end
end
end)
Takes forever to type on mobile
You are creating the Tweens within the scope of that function and are never stored or referenced outside of it, so everytime the function is called it’ll create new Tweens each time and will cancel them if number = 2
before they even start. If you want to cancel Tweens created on a previous function call you’re gonna have to store them outside of it within variables or tables.
Here’s an example of what I mean
--Foward declaration of a variable outside of the function will let each function called access it
local tween
local function func()
if tween then
tween:Cancel()
else
tween = TweenService:Create()
end
end)
I’m not sure if this works but if you don’t like module scripts, try this:
local TS = game:GetService('TweenService')
_G.SomeTween = TS:Create(Instance, TweenInfo.new(.2), {Position = Vector3.new(2,2,2)})
_G.SomeTween:Play()
Using _G. is not really a good practice and I’d recommend module scripts but if it’s an easier alternative.
If you don’t know what _G is; it’s a global variable that can be accessed from any server script (if you define it in a server script) and any local script (if you define it in a local script).
Thank you for your time and responding on mobile! However it doesn’t seem to work. Have I done something wrong?
local number = 0
game.ReplicatedStorage.EquipPet.OnServerEvent:Connect(function(player)
number = number + 1
for i, attachmentInHumRP in pairs(player.Character.HumanoidRootPart:GetChildren()) do
if attachmentInHumRP.Name == "Attachment1" then
local petAttachment = attachmentInHumRP
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1)
local upTween
local downTween
local function tween()
if upTween or downTween then
upTween:Cancel()
downTween:Cancel()
else
upTween = TweenService:Create(petAttachment, tweenInfo, {
Position = Vector3.new(petAttachment.Position.X, 1.5, petAttachment.Position.Z),
Orientation = Vector3.new(petAttachment.Orientation.X, petAttachment.Orientation.Y, -5)
})
downTween = TweenService:Create(petAttachment, tweenInfo, {
Position = Vector3.new(petAttachment.Position.X, 0, petAttachment.Position.Z),
Orientation = Vector3.new(petAttachment.Orientation.X, petAttachment.Orientation.Y, 5)
})
end
end
tween()
if number == 2 then
attachmentInHumRP.Position = Vector3.new(-3.152,1.5,6.566)
attachmentInHumRP.Orientation = Vector3.new(0,-115,0)
end
upTween.Completed:Connect(function()
downTween:Play()
end)
downTween.Completed:Connect(function()
upTween:Play()
end)
upTween:Play()
end
end
end)
Those variables are still within the same scope as the function itself.
Place the variables outside of the function being connected to the Event instead:
local upTween
local downTween
game:GetService("ReplicatedStorage").EquipPet.OnServerEvent:Connect(function(player)
if upTween or downTween then
upTween:Cancel()
downTween:Cancel()
end
--Create Tweens
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.