尝试使用Position访问nil

我正在尝试类似投掷矛的东西,但我很困惑。它显示:

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

无论如何,这是LocalScript代码:

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)

和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)

我正在跟随教程,但我不知道问题如何出现。

点赞
用户2860267
用户2860267

你的错误提示指出你正在尝试引用一个不存在的对象上的字段。在这种情况下,它是´Mouse´对象,你没有从客户端提供它。

要解决这个问题,在调用RemoteEvent的FireServer()函数时传递鼠标信息。

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)
2020-12-04 20:23:41