服务器脚本无法在工作区中找到部件

问题描述 投票:0回答:1

我正在制作 PvP 游戏并且拥有火球能力。我试图在火球接触人形机器人后将其摧毁,但是脚本无法在工作区中找到火球部分并给出无限产量警告。

这是创建火球对象的代码:

    if Input.KeyCode == Enum.KeyCode.Q  then
    if debounce == true then return end
    debounce = true
    local player = game.Players.LocalPlayer
    local lookdirection = player.Character.HumanoidRootPart.CFrame.LookVector
    
    
    
    print(player, "uses Fireball")
    local fireball = game.ReplicatedStorage.Animations.FireBall.Fireball:Clone()
    fireball.Parent = workspace.Temp
    fireball.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-2)
    fireball.AssemblyLinearVelocity = lookdirection * 50
    fireball:SetAttribute("Owner", player.Name)
    
    local Force = Instance.new("BodyForce")

    Force.Force = Vector3.new(0, workspace.Gravity * fireball:GetMass() , 0)
    Force.Parent = fireball
    
    game.ReplicatedStorage.FireBall:FireServer(fireball.Size, fireball.CFrame, fireball.AssemblyLinearVelocity)
    
    wait(cooldown)
    debounce = false
    
end

当玩家按下 Q 时,脚本会从 ReplicatedStorage 克隆一部分并将其放入workspace.Temp 文件夹中,然后触发 Fireball RemoteEvent。

这是 RemoteEvent 的代码:

game.ReplicatedStorage.FireBall.OnServerEvent:Connect(function(player)

local fireball = game.Workspace.Temp:WaitForChild("Fireball")
local hitbox = Instance.new("Part")
hitbox.Parent = workspace.Hitboxes
hitbox.Anchored = false
hitbox.CanCollide = false
hitbox.Transparency = 0.5
hitbox.Size = fireball.Size
hitbox.CFrame = fireball.CFram
hitbox.AssemblyLinearVelocity = fireball.AssemblyLinearVelocity
local Force = Instance.new("BodyForce")

Force.Force = Vector3.new(0, workspace.Gravity * hitbox:GetMass() , 0)
Force.Parent = hitbox


--workspace.Temp:WaitForChild("Fireball").CFrame
game.Debris:AddItem(hitbox, 2)

local Hits = {}
hitbox.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= player.Name then
        if Hits[hit.Parent.Name] then
            return
        end

        if hit.Parent:GetAttribute("Block")  then
            print(hit.Parent.Name, "blocks the attack")
        else

            print(hit.parent.Name, ("HIT BY FIREBALL"))
            hit.Parent.Humanoid:TakeDamage(10)

        end

        Hits[hit.Parent.Name] = true
        hitbox:Destroy()
        fireball:Destroy()
        
        wait(4)
    end
end)

完)

我试图将火球对象本身传递到RemoteEvent中,但是我无法让它工作,并且无法找到关于是否可以在线的直接答案,所以相反 我有 WaitForChild 方法在工作区中查找对象,但如前所述,它只返回无限的产量。我可以在游戏中看到文件夹中的火球对象,并且我确信拼写是正确的,所以我有点无能为力。 如有任何帮助,我们将不胜感激。

roblox roblox-studio
1个回答
0
投票

这不起作用的原因是因为您正在客户端上创建火球,然后在服务器上尝试从文件夹中获取它。这将不起作用,因为为了对抗漏洞,如果您在客户端上创建一个实例,它将不会复制到服务器。

由于该部分没有被复制,WaitForChild将无限期地产生。

您可以调用 RemoteEvent,而不是在客户端创建火球,然后服务器应该创建火球实例。

添加注释,您可以通过 RemoteEvents 传递实例,只要实例在客户端和服务器上都复制即可。

© www.soinside.com 2019 - 2024. All rights reserved.