So I'm working on a tool that plays animations. I'm trying to make it so it can play animations by clicking or keypresses. The clicking part works great, but not so much with the keypressing. I'm trying to define "mouse" so that my function that runs a keypress animation (Sprinting in this case) can run, but I receive the following error:

15:45:07.645 - Players.Player1.Backpack.Sword RPG  admin.SwordScript:271: attempt to index local 'mouse' (a nil value)
15:45:07.646 - Stack Begin
15:45:07.647 - Script 'Players.Player1.Backpack.Sword RPG  admin.SwordScript', Line 271
15:45:07.647 - Stack End

Here's the script section that defines several things.

function OnEquipped()
	Delay(0.55, function() UnsheathSound:play() end)
	MyCharacter = Tool.Parent
	player = PlayersService:GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	mouse = player:GetMouse()
	MyHumanoid.WalkSpeed = 20
	PlaySlash = WaitForChild(Tool, 'PlaySlash')
	PlayOverhead = WaitForChild(Tool, 'PlayOverhead')
	PlayThrust = WaitForChild(Tool, 'PlayThrust')
	PlaySprint = WaitForChild(Tool, 'PlaySprint')
	
end

Note that outside of that function, at the top of the script, these are also (re?)defined like this so everything else can use them for reference:

local MyCharacter
local MyHumanoid
local player
local mouse

Thanks.

EDIT: Full script:

-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end


local DebrisService = Game:GetService("Debris")
local PlayersService = Game:GetService('Players')
local damage = 10


local SLASH_DAMAGE = 45
local LUNGE_DAMAGE = 60
local OVERHEAD_SLASH_DAMAGE = 100
local CAN_CUT_TREES = true

local Tool = script.Parent
local Handle = Tool.Mesh

local KILL_FEED_ICON_URL = 'http://www.roblox.com/asset/?id=94746323'


local MyCharacter
local MyHumanoid
local player
local mouse


local PlaySlash
local PlayThrust
local PlayOverhead
local PlaySprint

local AttackSequence = {}
local SlashSound = Instance.new("Sound")
SlashSound.SoundId = "rbxasset://sounds\\swordslash.wav"
SlashSound.Parent = Handle
SlashSound.Name = 'SlashSound'
SlashSound.Volume = .3

local OverheadSound = Instance.new("Sound")
OverheadSound.SoundId = "rbxasset://sounds\\swordslash.wav"
OverheadSound.Parent = Handle
OverheadSound.Name = 'OverheadSound'
OverheadSound.Pitch = 1.17
OverheadSound.Volume = 0.42

local LungeSound = Instance.new("Sound")
LungeSound.SoundId = "rbxasset://sounds\\swordlunge.wav"
LungeSound.Parent = Handle
LungeSound.Name = 'LungeSound'
LungeSound.Volume = .25
LungeSound.Pitch = 0.85

local UnsheathSound = Instance.new("Sound")
UnsheathSound.SoundId = "rbxasset://sounds\\unsheath.wav"
UnsheathSound.Parent = Handle
UnsheathSound.Name = 'UnsheathSound'
UnsheathSound.Volume = 0.14


local HitCharacters

local CreatorTag = nil

local function CheckInTreesModel(object)
	if object == nil or not Workspace:FindFirstChild('Trees') or not object.Parent or object.Parent.Name == 'Billboard' then return false end
	return object:IsDescendantOf(Workspace.Trees)
end

function Attack()
	damage = SLASH_DAMAGE
	SlashSound:play()

	if PlaySlash then
		PlaySlash.Value = not PlaySlash.Value
	end
	wait(0.6)
end

function OverheadSlash()
	
	if PlayOverhead then
		PlayOverhead.Value = not PlayOverhead.Value
	end
	--OverheadSound:Play()
LungeSound:play()
	
	wait(0.6)
end

function Lunge()
	--LungeSound:play()
	OverheadSound:Play()
	if PlayThrust then
		PlayThrust.Value = not PlayThrust.Value
	end
	
	wait(.6)
	

	
end


function Sprint(q)
	
	if PlaySprint then
		PlaySprint.Value = not PlaySprint.Value
	end
	if PlaySprint then
		if PlaySprint.Value==true then
			MyHumanoid.WalkSpeed = 31
			else MyHumanoid.WalkSpeed =16
		end
	end
	--OverheadSound:Play()
--LungeSound:play()
	
	
end


function swordUp()
	Tool.GripForward = Vector3.new(1,0,0)
	Tool.GripRight = Vector3.new(0,0,1)
	Tool.GripUp = Vector3.new(0,1,0)
end

function swordOut()
	Tool.GripForward = Vector3.new(0,-1,0)
	Tool.GripRight = Vector3.new(0,0,1)
	Tool.GripUp = Vector3.new(-1,0,0)
end

table.insert(AttackSequence, Attack)
table.insert(AttackSequence, Lunge)
table.insert(AttackSequence, OverheadSlash)

local CurrentIndex = 1

local LastAttackTime = tick()

Tool.Enabled = true
function OnActivated()
	if not Tool.Enabled then
		return
	end
	Tool.Enabled = false
	HitCharacters = {[MyCharacter] = true}
	----------- ATTACK SEQUENCE CODE --------------
	local now = tick()
	if now - LastAttackTime > 1.5 then
		CurrentIndex = 1
	else
		CurrentIndex = CurrentIndex + 1
	end
	LastAttackTime = now
	if CurrentIndex > #AttackSequence then
		CurrentIndex = 1
	end

  -----------------------------------------------
	AttackSequence[CurrentIndex]()
	if MyHumanoid == nil then
		print("Humanoid not found")
		return 
	end
	HitCharacters = nil
	Tool.Enabled = true
end


function OnEquipped()
	Delay(0.55, function() UnsheathSound:play() end)
	MyCharacter = Tool.Parent
	player = PlayersService:GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	mouse = player:GetMouse()
	MyHumanoid.WalkSpeed = 20
	PlaySlash = WaitForChild(Tool, 'PlaySlash')
	PlayOverhead = WaitForChild(Tool, 'PlayOverhead')
	PlayThrust = WaitForChild(Tool, 'PlayThrust')
	PlaySprint = WaitForChild(Tool, 'PlaySprint')
	
end

function OnUnequipped()
	MyHumanoid.WalkSpeed = 16
end

-- Also activate when the Fire Button is down
local function OnChildAdded(child)
	if child.Name == 'FireButtonDown' then
		child.Changed:connect(function(newValue)
			if newValue == true then
				OnActivated()
			end
		end)
	end
end



Tool.Activated:connect(OnActivated)
Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)

--connection = Handle.Touched:connect(Blow)

Tool.ChildAdded:connect(OnChildAdded) --NOTE: Added for Fire Button
mouse.KeyDown:connect(Sprint)

3 comments

0

This looks like a normal Script. Do note that the Mouse object can only be accessed through a LocalScript.

0

Switched it to a LocalScript, had the same problem. Putting the full script, that might help.

0

Also, I'm trying to get mouse so I can use keys to do animations aside from the main attacks. The Sprint function at 107 is one such animation.