简体   繁体   中英

Attempt to index nil with Position

I'm trying to do something like a spear throw and I'm so confused. It says:

ServerScriptService.FireMagic.FireSpear:16: attempt to index nil with 'Position'

Anyways, here's the LocalScript code:

wait(1)

local Player = game.Players.LocalPlayer
local Character = Player.Character

local Mouse = Player:GetMouse()

local rp = game:GetService("ReplicatedStorage")
local FireSpear = rp:WaitForChild("FireRemote")

local UIS = game:GetService("UserInputService")

local debounce = true
local cd = 10

UIS.InputBegan:Connect(function(input, isTyping)
    if isTyping then
        return
    elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
        debounce = false
        
        FireSpear:FireServer()
        
        wait(cd)
        debounce = true
    end
end)

and the Script:

wait(1)

local rp = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")

local ssFireSpear = ss.FireMagic:WaitForChild("ssFireSpear")
local FireRemote = rp:WaitForChild("FireRemote")

local UhTable = {}

local function LookatMouse(Mouse, RootPart)
    local bodyG = Instance.new("BodyGyro")
    bodyG.MaxTorque = Vector3.new(0, 500000, 0)
    bodyG.P = 10000
    bodyG.CFrame = CFrame.new(RootPart.Position, Mouse.Position)
    bodyG.Parent = RootPart
    Debris:AddItem(bodyG, 1)
    
end

local function MoveTowardsMouse(Mouse, Main)
    local bodyV = Instance.new("BodyVelocity")
    bodyV.MaxForce = Vector3.new(500000, 500000, 500000)
    bodyV.Velocity = CFrame.new(Main.Position, Mouse.Position).LookVector * 100
    bodyV.Parent = Main
    
    local bodyG = Instance.new("BodyGyro")
    bodyG.MaxTorque = Vector3.new(500000, 500000, 500000)
    bodyG.P = 10000
    bodyG.CFrame = CFrame.new(Main.Position, Mouse.Position)
    bodyG.Parent = Main
end

FireRemote.OnServerEvent:Connect(function(Player, Mouse_CFrame)
    if UhTable[Player.Name] == true then
        return
    end
    
    UhTable[Player.Name] = true
    
    local Character = Player.Character
    local RootPart = Character:WaitForChild("HumanoidRootPart")
    
    local folder = workspace:FindFirstChild("DebrisFolder") or Instance.new("Folder",workspace)
    folder.Name = "DebrisFolder"
    
    local RightHand = Character:WaitForChild("RightHand")
    
    local FireSpear = ssFireSpear:Clone()
    
    local Handle = FireSpear:WaitForChild("Handle")
    local Hitbox = FireSpear:WaitForChild("Hitbox")
    local Mesh = FireSpear:WaitForChild("Mesh")
    FireSpear:SetPrimaryPartCFrame(RightHand.CFrame)
    FireSpear.Parent = folder
    
    local weld = Instance.new("Motor6D")
    weld.Parent = Handle
    weld.Part0 = RightHand
    weld.Part1 = Handle
    
    Hitbox:SetNetworkOwner(nil)
    
    local function MakeStuffHappen()
        spawn(function()
            LookatMouse(Mouse_CFrame,RootPart)
            wait(.6)
            weld:Destroy()
            MoveTowardsMouse(Mouse_CFrame,Hitbox)
        end)
    end
    
    MakeStuffHappen()
    
end)

I'm following a tutorial but I don't know how the issue got there.

Your error is pointing to the fact that you are trying to reference fields on an object that doesn't exist. In this case, it's the ´Mouse´ object, which you never supplied from the client.

To fix this, pass the mouse information in when you call the RemoteEvent's FireServer() function.

UIS.InputBegan:Connect(function(input, isTyping)
    if isTyping then
        return
    elseif input.KeyCode == Enum.KeyCode.E and debounce and Character then
        debounce = false
        local mouseCFrame = Mouse.Hit
        FireSpear:FireServer(mouseCFrame)
        
        wait(cd)
        debounce = true
    end
end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM