Help with an Animation Toggled with a keybind

wait… to add on to what I just said, it appears that I am no longer having my messages print.

Can you paste your script here?

wait… now its
working, but the animation is doing… whatever this is?


corrupted animation. I remade it and it appear to be working now. Thank you SO MUCH and sorry for taking up so much of you guys’ time on this.

Great! glad it’s working. If you’d be so kind and mark my response as a solution, that’d be great. Cheers.

1 Like

Yeah, of course! One last thing, could you possibly help with the clearing backpack and giving back the items portion? I have already taken up so much of your time so It’s obviously okay if you say no LOL
image
@Katrist I tried but I still don’t really understand a lot about the backpack in general

1 Like

I’ll do a basic design for you or Primo to use for help.

– Clearing backpack
You’ll want to save a log of every item in the backpack (possibly by cloning the backpack and keeping that clone in a dictionary)
You’ll clear all items in the player’s backpack

– Retrieving backpack
You’ll look through the dictionary for the player
Reparent the backpack found (or just its objects) to the player

1 Like

Thank you, this will really help! also I genuinely want to thank you for adding a “try it yourself” portion, because its hard to learn when someone gives a 100% complete script. Like, actually, It sets me back in learning if I get pushed a solution where I understand nothing. :slight_smile:

1 Like

for the saving portion, would I use a datastore of some form?

Unless you want their items to be saved across sessions, just use a dictionary.

Ty, will try that out shortly.

I do understand how to use dictionary, but I am not entirely sure as to how I would save the backpack using this. Would I have to name every item or is there just like a backpack contents

I’ve done the code for you and left comments. Make sure to read them for a full understanding.

Code:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Surrender = ReplicatedStorage:WaitForChild("Surrender")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://15687505843"

local surrenderedPlayers = {}
local surrenderAnimations = {}
local playerBackpacks = {}

Surrender.OnServerEvent:Connect(function(player)
	local humanoid = player.Character and player.Character:FindFirstChildWhichIsA("Humanoid")

	if not humanoid then
		return
	end

	local hasSurrendered = surrenderedPlayers[player]

	if hasSurrendered then
		surrenderedPlayers[player] = false
	else
		surrenderedPlayers[player] = true
	end

	if surrenderedPlayers[player] then
		local surrenderAnimation = humanoid.Animator:LoadAnimation(animation)
		surrenderAnimation:Play()

		surrenderAnimations[player] = surrenderAnimation
		
		-- Unequip current tools
		player.Character.Humanoid:UnequipTools()
		
		-- Create dictionary table
		playerBackpacks[player] = {}
		
		-- Insert all tools into table and destroy tools
		for i, child in player.Backpack:GetChildren() do
			table.insert(playerBackpacks[player], child:Clone())
			child:Destroy()
		end
	else
		surrenderAnimations[player]:Stop()

		-- Give items back
		for i, tool in playerBackpacks[player] do
			tool.Parent = player.Backpack
		end
		
		-- Clear dictionary
		playerBackpacks[player] = nil
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function()
		surrenderedPlayers[player] = nil
		surrenderAnimations[player] = nil
		playerBackpacks[player] = nil
	end)
end)

If it works, a solution would be appreciated.

1 Like

Works, thank you so MUCH, and I will be sure to read the comments!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.