简体   繁体   English

尝试使用 Position 索引 nil

[英]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' ServerScriptService.FireMagic.FireSpear:16:尝试使用“位置”索引 nil

Anyways, here's the LocalScript code:无论如何,这是 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)

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.您的错误表明您正在尝试引用不存在的 object 上的字段。 In this case, it's the ´Mouse´ object, which you never supplied from the client.在这种情况下,它是您从未从客户端提供的“鼠标”object。

To fix this, pass the mouse information in when you call the RemoteEvent's FireServer() function.要解决此问题,请在调用 RemoteEvent 的 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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