So I’m making a light saber for a friend, and had to follow a tutorial for some of the scripting. But for what ever reason my animation is not working.
I also would like to add sound to this but not sure how to do that with out messing up the other code. For the sound I want it to play a sound when I press “j” to turn it on and a different sound when I click on my screen.
local uis = game:GetService("UserInputService")
local tool = script.Parent
local attack = tool:WaitForChild("attack")
local player = game.Players.LocalPlayer
local equipped = false
local on = false
tool.Activated:Connect(function()
if not on then return end
local animation = player.Character.Humanoid:LoadAninmation(attack)
animation:Play()
end)
tool.Equipped:connect (function ()
equipped = true
end)
tool.Unequipped:connect (function ()
equipped = false
end)
local function turnOff()
on = false
tool.RemoteEvent:FireServer(false)
end
local function turnOn()
on = true
tool.RemoteEvent:FireServer(true)
end
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
else
turnOn()
end
end
end)
it depends what sound you want to use. If the sound is for turning on the light saber, put it in the place that receives the remote event as “true”. If the sound is for the light saber attack, put it right before the animation.
If the input thingy works, then all you need to do is input the sound in the following:
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
--here for when turned off
else
turnOn()
--here for when turned on
end
end
end)
I think you can just put the sound object in the tool, or the player. Here’s what it would look like in the tool:
local sound = tool.sound --Note: sound is the example name of the sound. replace it with whatever your sound name is. Also remember to actually add a sound object in the tool and input the correct id.
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
sound:Play() --Play the sound when the thing turns off
--here for when turned off
else
turnOn()
--here for when turned on
sound:Play() --Play the sound when the thing turns on
end
end
end)
Here’s the Sound article from DevHub in case you need it.
Question- do you just want to know when to use the sound? or do you need the thing where you press J part as well? looks like you’ve already got it here:
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
else
turnOn()
end
end
end)```
find the similar part in your code and replace it. (aka, this part)
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
else
turnOn()
end
end
end)
The “press J to turn off/on” works right? if so just do that
local uis = game:GetService("UserInputService")
local tool = script.Parent
local attack = tool:WaitForChild("attack")
local player = game.Players.LocalPlayer
local equipped = false
local on = false
tool.Activated:Connect(function()
if not on then return end
local animation = player.Character.Humanoid:LoadAnimation(attack)
animation:Play()
end)
tool.Equipped:connect (function ()
equipped = true
end)
tool.Unequipped:connect (function ()
equipped = false
end)
local function turnOff()
on = false
tool.RemoteEvent:FireServer(false)
end
local function turnOn()
on = true
tool.RemoteEvent:FireServer(true)
end
local sound = tool.lightsaberOn
uis.InputBegan:Connect(function (input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.J and equipped then
if on then
turnOff()
sound:Play() --Play the sound when the thing turns off
--here for when turned off
else
turnOn()
--here for when turned on
sound:Play() --Play the sound when the thing turns on
end
end
end)