Hello,
Last night I created this script for a lightsaber, it seems to work well but randomly runs the function SaberOff().
It comes from when the tool is unequipped.
Here’s the code:
local function SaberOff()
idle:Stop()
Blocking = false
Attacking = false
Stunned = false
script.Parent.RemoteEvent:FireServer("BlockUp",nil)
block:Stop()
hum.WalkSpeed = 16
disable:Play()
script.Parent.RemoteEvent:FireServer("Off",nil)
Tool.Handle.Close:Play()
Tool.Handle.Hum:Stop()
Phase = "Waiting"
Idle = false
local T = 20
repeat
wait(0.05)
hum:EquipTool(Tool)
T -= 1
until T == 0
disable:Stop()
Phase = "Off"
print("Off")
end
local function SaberOn()
ignite:Play()
script.Parent.RemoteEvent:FireServer("Ignite",nil)
Tool.Handle.Ignite:Play()
Phase = "Waiting"
Idle = true
local T = 40
repeat
wait(0.05)
hum:EquipTool(Tool)
T -= 1
until T == 0
ignite:Stop()
Tool.Handle.Hum:Play()
Phase = "On"
idle:Play()
print("Saber on")
end
local function SaberAttack()
print("Attacking player.")
end
local function SaberSwing(Object)
if Attacking == true then
if Object.Parent:FindFirstChild("Humanoid") then
if Object.Parent.Blocking.Value == false then
Attacking = false
SaberAttack(Object.Parent.Humanoid)
script.Parent.RemoteEvent:FireServer("Attack",Object.Parent)
Tool.Handle.Swing:Stop()
Tool.Handle.Attack:Play()
print("Attacked")
elseif Object.Parent.Blocking.Value == true then
script.Parent.RemoteEvent:FireServer("BlockUp",nil)
Tool.Handle.Swing:Stop()
Tool.Handle.Attack:Stop()
Stunned = true
stun:Play()
hum.WalkSpeed = 10
wait(1)
hum.WalkSpeed = 16
stun:Stop()
Stunned = false
print("Stunned")
end
elseif Object.Name == "_Wires" then
print("Cut wires.")
end
end
end
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Q then
if Phase == "Off" and Stunned == false and Blocking == false and Swinging == false then
SaberOn()
elseif Phase == "On" and CanTurnOff == true and Stunned == false and Blocking == false and Swinging == false then
SaberOff()
end
end
end)
local AttackDB = false
Mouse.Button1Down:connect(function()
if Idle == true and AttackDB == false and Blocking == false then
Swinging = true
AttackDB = true
CanTurnOff = false
Tool.Handle.Swing:Play()
Attacking = true
attack1:Play()
wait(1)
attack1:Stop()
Attacking = false
AttackDB = false
Swinging = false
CanTurnOff = true
end
end)
Mouse.Button2Down:connect(function()
if Idle == true and Attacking == false and Blocking == false and Stunned == false then
Blocking = true
block:Play()
script.Parent.RemoteEvent:FireServer("BlockDown",nil)
Tool.Handle.Swing:Play()
hum.WalkSpeed = 10
end
end)
Mouse.Button2Up:connect(function()
if Idle == true and Attacking == false and Blocking == true then
Blocking = false
block:Stop()
script.Parent.RemoteEvent:FireServer("BlockUp",nil)
Tool.Handle.Swing:Play()
hum.WalkSpeed = 16
end
end)
Tool.Lightsaber.Blade1.Touched:Connect(function(Object)
SaberSwing(Object)
end)
local function Close()
local checkofready = false
if Phase == "On" and Stunned == true or Blocking == true or Attacking == true or Swinging == true then
wait(0.1)
hum:EquipTool(Tool)
checkofready = true
elseif Phase == "On" and CanTurnOff == true and checkofready == false then
SaberOff()
print("Saber turning off")
end
end
Tool.Unequipped:connect(Close)
Any help would be appreciated. Thanks!