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

How would i add sound to this script? So when i press a button the sound activates with the script.

Keep in mind im not inserting a sound in workspace i want to do it through a script.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
enabled = false

local animation = Instance.new("Animation",script)
animation.AnimationId = "http://www.roblox.com/asset/?id=01311925674t"
local animation2 = Instance.new("Animation",script)
animation2.AnimationId = "http://www.roblox.com/asset/?id=903173770"
mouse.KeyDown:connect(function(key)
	if key == "z" and enabled == false then --turns on
	enabled = true
	local UI = player.Character.Humanoid:LoadAnimation(animation)
	UI:Play()
	wait(3)--However long your animation is basically
	local SSJParticle = game.ReplicatedStorage.Particles.UI:Clone()
	SSJParticle.Parent = player.Character.HumanoidRootPart
	local SSJHair = game.ReplicatedStorage.Hairs.UI:Clone()
	SSJHair.Parent = player.Character
	SSJHair.CFrame = player.Character.HumanoidRootPart.CFrame
	local Weld = Instance.new("Weld")
	Weld.Name = "UI"
	Weld.Parent = player.Character.Head
	Weld.Part0 = player.Character.Head
	Weld.Part1 = SSJHair
	Weld.C0  = CFrame.new(0, .9, 0.11) * CFrame.Angles(0,math.pi,0)
	player.Character.Humanoid.MaxHealth = 50000
	player.Character.Humanoid.Health = 50000
	player.Character.Humanoid.WalkSpeed = 80
	else if key == "z" and enabled then-- turns off.
	local SSJ2 = player.Character.Humanoid:LoadAnimation(animation2)
	SSJ2:Play()
	player.Character.Humanoid.WalkSpeed = 16
	player.Character.Humanoid.MaxHealth = 200
	player.Character.Humanoid.Health = 200
	wait(1)
	enabled = false
	end
	end
end)

1 answer

First off, KeyDown is deprecated so use UserInputService. Second off, just use a play function like so:

local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService") --Calls upon UserInputService
local enabled = false

local animation = Instance.new("Animation",script)
animation.AnimationId = "http://www.roblox.com/asset/?id=01311925674t"
local animation2 = Instance.new("Animation",script)
animation2.AnimationId = "http://www.roblox.com/asset/?id=903173770"
local sound = whereversoundis -- change this to where your sound is located although you can also use Instance.new

uis.InputBegan:Connect(function(obj, gp) --Function for when key is pressed
    if obj.KeyCode == Enum.KeyCode.Z and not gp then --checks if the key is z

enabled = not(enabled)

if enabled == true then --does ssj
    local UI = player.Character.Humanoid:LoadAnimation(animation)
    UI:Play()
    wait(3)
    local SSJParticle = game.ReplicatedStorage.Particles.UI:Clone()
    SSJParticle.Parent = player.Character.HumanoidRootPart
    local SSJHair = game.ReplicatedStorage.Hairs.UI:Clone()
    SSJHair.Parent = player.Character
    SSJHair.CFrame = player.Character.HumanoidRootPart.CFrame
    local Weld = Instance.new("Weld")
    Weld.Name = "UI"
    Weld.Parent = player.Character.Head
    Weld.Part0 = player.Character.Head
    Weld.Part1 = SSJHair
    Weld.C0  = CFrame.new(0, .9, 0.11) * CFrame.Angles(0,math.pi,0)
    player.Character.Humanoid.MaxHealth = 50000
    player.Character.Humanoid.Health = 50000
    player.Character.Humanoid.WalkSpeed = 80
	sound:Play() --Plays the sound

    else -- does ssj2

    local SSJ2 = player.Character.Humanoid:LoadAnimation(animation2)
    SSJ2:Play()
    player.Character.Humanoid.WalkSpeed = 16
    player.Character.Humanoid.MaxHealth = 200
    player.Character.Humanoid.Health = 200
 	sound:Play() --Plays sound again. If you want a different sound for ssj2, make another sound variable
    end
    end
end)

This is merely an example so, put the sound:Play() wherever you want it to be.

2 comments

0

"Players.Exarlus.PlayerGui.Ultra Instinct:15: '=' expected near 'enabled'"

0

I apologize about that. Try it now.

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