Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mouse is nil no matter what I do??

Asked by 7 years ago

This question already has an answer here:

Mouse is nil regardless of what I try?

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:

13:57:40.116 - Players.Player1.Backpack.Sword RPG  admin.SwordScript:271: attempt to index local 'mouse' (a nil value)
13:57:40.117 - Stack Begin
13:57:40.118 - Script 'Players.Player1.Backpack.Sword RPG  admin.SwordScript', Line 271
13:57:40.119 - 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 Blow(hit)
--  if (hit.Parent == nil) then return end -- happens when bullet hits sword
--  local humanoid = hit.Parent:findFirstChild("Humanoid")
--  local vCharacter = Tool.Parent
--  --local vPlayer = game.Players:playerFromCharacter(vCharacter)
--  local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
--  if humanoid~=nil and humanoid ~= hum and hum ~= nil and HitCharacters and not HitCharacters[humanoid.Parent] then
--      -- final check, make sure sword is in-hand
--      local right_arm = vCharacter:FindFirstChild("Right Arm")
--      if (right_arm ~= nil) then
--          local joint = right_arm:FindFirstChild("RightGrip")
--          if (joint ~= nil and (joint.Part0 == Handle or joint.Part1 == Handle)) then
--              HitCharacters[humanoid.Parent] = true
--              --TagHumanoid(humanoid, vPlayer)
--              --humanoid:TakeDamage(damage)
--              local hitBindable = humanoid:FindFirstChild('Hit')
--              if hitBindable then
--                  hitBindable:Invoke(damage, CreatorTag)
--              else
--                  print("Could not find BindableFunction 'Hit'")
--              end
--          end
--      end
--  elseif CheckInTreesModel(hit) and Tool.Enabled == false and CAN_CUT_TREES then
--      hit:BreakJoints()
--      if hit and not hit.Parent:FindFirstChild('Cut') then
--          if hit.Parent and hit.Parent:FindFirstChild('Ball') then
--              hit.Parent.Ball.Velocity =
--                  hit.Parent.Ball.Velocity +
--                   CFrame.Angles(0, -math.rad(math.random(0, 180)), 0) * ((hit.CFrame.p - MyCharacter.Torso.CFrame.p).unit:Cross(Vector3.new(0,1,0)) * 15)
--              local cut = Instance.new('BoolValue')
--              cut.Value = true
--              cut.Name = 'Cut'
--              cut.Parent = hit.Parent
--          else
--              print('No Ball')
--          end
--      end
--  end
--end
----[[
--function TagHumanoid(humanoid, player)
--  -- Add more tags here to customize what tags are available.
--  while humanoid:FindFirstChild('creator') do
--      humanoid:FindFirstChild('creator'):Destroy()
--  end 
--  local creatorTag = Instance.new("ObjectValue")
--  creatorTag.Value = player
--  creatorTag.Name = "creator"
--  DebrisService:AddItem(creatorTag, 1.5)
--  creatorTag.Parent = humanoid
--
--  local weaponIconTag = Instance.new("StringValue")
--  weaponIconTag.Value = KILL_FEED_ICON_URL
--  weaponIconTag.Name = "icon"
--  weaponIconTag.Parent = creatorTag
--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)

1 answer

Log in to vote
-1
Answered by 7 years ago

I think that there si a problem with the key reader.So try this:

local player = game.Players.LocalPlayer local mouse = player:GetMouse

function Sprint(key) if key == 'q' then -->your code end

mouse.KeyDown:connect(function(Sprint)

0
Doesn't work. At the bottom where you try to connect, it registers 'function' as trying to add an actual function. So, it expects you to add an end and doesn't work. :c SHmods_arebad 0 — 7y
0
Mouse events for key input should not be used (use UserInputService instead), "connect" is deprecated (use "Connect" instead), and when providing a code example in an answer, use code blocks. ScriptGuider 5640 — 7y
Ad

Answer this question