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

My custom animation stops playing as soon as another is loaded, how can i stop this?

I've created a script that teleports the player in the direction (only x and z value) they're looking at when they press E on the keyboard. It plays the animation like intended, but if the player changes their animation (i.e. Presses E while running, then stops before the animation finishes), the animation is replaced by the newly loaded idle animation. How can I make sure that the custom animation fully finishes before loading the default animations for running, idle, etc.?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportEvent = Instance.new("RemoteEvent", ReplicatedStorage)
TeleportEvent.Name = "TeleportEvent" 

local function onTeleport(plr)
	local char = game.Workspace:FindFirstChild(plr.Name)
	local Trail = game.Workspace.Trail:Clone()
	local Trail2 = game.Workspace.Trail:Clone()
	local humanoid = char.Humanoid
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://roblox.com/asset/?id=4543906727"
	local animTrack = humanoid:LoadAnimation(animation)
	animTrack:Play()
	wait()
	Trail.Parent = char.HumanoidRootPart
	            local attachment0 = Instance.new("Attachment", char.HumanoidRootPart)
	            attachment0.Name = "TrailAttachment0"
	            local attachment1 = Instance.new("Attachment", char.UpperTorso)
	            attachment1.Name = "TrailAttachment1"
	            Trail.Attachment0 = attachment0
	            Trail.Attachment1 = attachment1	
	            wait(1.25)
	            Trail:Remove()
end

TeleportEvent.OnServerEvent:Connect(onTeleport)

^This code is in a script inside of ServerScriptService

The function is called in a different script, which has some local visual effects and checks for the keyboard input.

Any help is appreciated!

3 comments

0

Instead of saying "local char = game.Workspace:FindFirstChild(plr.Name)" then just say local char = plr.Character

0

Hmm, the script still teleports the player and plays the animation, but I still have the same problem as before—the animation is stopped whenever the other animations are loaded

0

You can also add a debounce, so the animation can runs perfectly until the second one runs.

1 answer

--> I only edit somethings and add the debounce

local RS = game:GetService("ReplicatedStorage")
local teleportEvent = Instance.new("RemoteEvent", RS)
teleportEvent.Name = "TeleportEvent"
local debounce = false

local function onTeleport(player)
	local character = game.Workspace:FindFirstChild(player.Name)
	local trail1 = game.Workspace.Trail:Clone()
	local trail2 = game.Workspace.Trail:Clone()
	local humanoid = char:WaitForChild("Humanoid") --> Wait the humanoid
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://roblox.com/asset/?id=4543906727"
	local animTrack = humanoid:LoadAnimation(animation)
	animTrack:Play
	
	wait()

	if not debounce then
		trail1.Parent = character.HumanoidRootPart
		local attachment0 = Instance.new("Attachment", character.HumanoidRootPart)
		attachment.Name = "TrailAttachment0"
		local attachment1 = Instance.new("Attachment", character.HumanoidRootPart)
		attachment1.Name = "TrailAttachment1"
		trail.Attachment0 = attachment0
		trail.Attachment1 = attachment1
		wait() --> Put your wait time
		trail:Remove()
	else
		print("The code isn't correct") --> A print function can save your code
	end

	debounce = true
	wait() --> Put the time you want to run the animation perfectly
	debounce = false
	wait() --> Whatever you want to run again the function
end

local warnMessage = "All the code is incorrect"
pcall(warnMessage)
--> To verify there isn't a error on the code.

teleportEvent .OnServerEvent:Connect(onTeleport)

2 comments

0

Thanks for the suggestion! Unfortunately, this doesn't stop Roblox's default animations from playing over my own, it only prevents my animations from playing over my other animations. Is there a way to just temporarily stop the Roblox's default animations until my animation has fully finished?

0

I don't know. If I discover it I will let you know.

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