How to i add an animation on tool activation?

local model = script.Parent
local Debounce = true
local Delay = 5
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild(“Humanoid”)
local AnAnimation = model.Animation
local animTrack = humanoid:LoadAnimation(AnAnimation)
script.Parent.Activated:Connect(function()
if Debounce then
Debounce = false

  animTrack:Play()

model.Handle.BombBeeping:Play()
redlight = Instance.new(“SpotLight”)
redlight.Color = Color3.fromRGB(255,0,0)
redlight.Parent = model.Handle
redlight.Brightness = 100
redlight.Range = 100
redlight.Face = “Bottom”
wait(1)
model.Handle.BombBeeping:Stop()
model.Handle.Boom:Play()
model.MainVest.CanCollide = false
ex = Instance.new(“Explosion”)
ex.Position = script.Parent.MainVest.Position
ex.Parent = workspace
ex.BlastRadius = 10
redlight:Destroy()
wait(5)
Debounce = true
end
end)

The code seems correct. It may be an asset loading issue, are there any errors in the output? Do you own the animation?

16:00:48.112 - Players.vovcher.Backpack.Suicide Vest.ShrapnelLauncher:5: attempt to index local ‘player’ (a nil value)

also i own the animation.

1 Like

Try this:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Tool = script.Parent
local Animation = Tool:WaitForChild("Animation")
local AnimationTrack = Humanoid:LoadAnimation(Animation)

local Debounce = true
local Delay = 5

local function OnActivated()
	if Debounce then
		Debounce = false
		AnimationTrack:Play()
		Tool.Handle.BombBeeping:Play()
		local redlight = Instance.new("SpotLight")
		redlight.Color = Color3.fromRGB(255,0,0)
		redlight.Parent = model.Handle
		redlight.Brightness = 100
		redlight.Range = 100
		redlight.Face = "Bottom"
		wait(1)
		Tool.Handle.BombBeeping:Stop()
		Tool.Handle.Boom:Play()
		Tool.MainVest.CanCollide = false
		local ex = Instance.new("Explosion")
		ex.Position = script.Parent.MainVest.Position
		ex.Parent = workspace
		ex.BlastRadius = 10
		redlight:Destroy()
		wait(Delay)
		Debounce = true
	end
end

Tool.Activated:Connect(OnActivated)

I cleaned it up a bit

Also, please read this guide in posting code on the forum:

1 Like

16:35:07.809 - Players.vovcher.Backpack.Suicide Vest.ShrapnelLauncher:3: attempt to index local ‘Player’ (a nil value)

1 Like

Do you have another variable called “Player” or something that changes what the variable Player is? As far as I know, that code is supposed to be working fine :confused:

Edit:
Is this a local script or normal script?

this isnt a local script, its a normal script. (btw i dont have any other variable that changes the player variable)

This is the issue then. Change this to a local script and it should work fine.

Players.LocalPlayer is only accessible to local scripts.

it works but, The redlight and the explosion are local meaning that they cannot be seen by others and also it cannot effect others. What do i do to change it so the light and the explosion werent local??

also if the player equipped the tool, is the tool’s parent the player?

If you really need to use a server script, and the tool is in StarterPack, you can index Player as:

local player = script.Parent.Parent.Parent -- Tool>Backpack>Player

You could use remote events to tell the server that you activated the script and add sanity checks to avoid exploits.

@20amir02 It’s better to use a local script, as special effects such as explosions may add up to server load or impact performance on the server. Using a local script and remote events will allow a client-server-client communication so that the server never has to handle the special effects and sacrifice performance.

Edit:
I’m creating a demo place file, pleaase wait.

1 Like

Here’s an example of how to show other players effects that are happening locally:
Special Effects Router.rbxl (19.4 KB)

The good thing about this is, the server does not sacrifice any performance because everything (explosions, lights, sounds, etc) is being rendered on the clients and not the server. It’s a good practice to only show effects locally and never the server!

If you have questions about this, feel free to ask.

It’s actually a bad thing because the explosion is supposed to effect people, instead it effects the local user.

The explosion will affect other players. It’s the whole point of my demo file. The explosion will happen locally, and the server will tell other clients to create the same explosion for them locally. The server never has to have the explosion object. You just need to add sanity checks on the server to avoid exploiters taking advantage of disabling the replication script. If the players are within the radius of the explosion, they should have taken damage, else the server will force kill those players etc.

can u edit ur demo file so it also adds a spotlight and an animation?

I’d just like to remind you that asking people to create a whole script is not allowed.

My demo file was just a reference to give you an idea, not for me to do all the work. However since I have time, I did my best to make it do what the original script does. I have no idea how the environment of your game works or how it looks like, so expect something not to work right away (probably just object names)
Special Effects Router.rbxl (19.8 KB)

I have provided more than enough resources for this thread. It’s up to you on how to use them.

Errors, This is the error i got:

Players.vovcher.PlayerScripts.ShrapnelLauncher:40: bad argument #3 to ‘Position’ (Vector3 expected, got Object)

Change that line to this

ex.Position = Target.HumanoidRootPart.Position

I forgot to specifically target the position property of the humanoid root part

Thank you this works, But there is a problem it doesn’t work after its been used i dont know why it doesnt work there are no errors.
After you use it you die of the explosion, But when you respawn you can’t use it again even after the debounce. There are no errors nor infinite yields and i dont know the problem.