【问题标题】:Attempt to index nil with Position尝试使用 Position 索引 nil
【发布时间】:2021-03-16 09:39:27
【问题描述】:

我正在尝试做类似投掷长矛之类的事情,但我很困惑。它说:

ServerScriptService.FireMagic.FireSpear:16: 尝试使用 'Position' 索引 nil

无论如何,这是 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)

和脚本:

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)

我正在关注一个教程,但我不知道问题是如何出现的。

【问题讨论】:

  • MouseRootPartnil
  • 如果是的话,我相信会弹出一个红色的kine,或者在脚本分析中,说unknown global
  • 如果你索引一个本地 nil 值,为什么它会说“未知全局”?你相信什么是无关紧要的。在第 16 行中,您使用 Position 索引了两件事。鼠标和 RootPart。所以确保它们不是零

标签: lua roblox


【解决方案1】:

您的错误表明您试图引用不存在的对象上的字段。在本例中,它是您从未从客户端提供的“鼠标”对象。

要解决此问题,请在调用 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)

【讨论】:

  • 在我的情况下,我需要在按键后获得鼠标位置,这是在按键之后还是之前获得位置?
  • 之后。首先,任何输入都会触发此回调。如果那个输入是“E”,接下来鼠标在 3d 空间中的位置被采样,最后 FireRemote RemoteEvent 被触发到服务器。
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 2022-01-05
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多