The block is not being cloned and no error message is showing.(Roblox Studio)

我正在制作一个放置系统(目前还很基础),但它没有放置任何东西。代码可以正常工作,直到“while whenclicked = true”部分,以下是代码:

print('works')
while true do
    print('works2')
    local ImportedValueX = game.ReplicatedStorage.ActPosX
    local ImportedValueY = game.ReplicatedStorage.ActPosY
    local ImportedValueZ = game.ReplicatedStorage.ActPosZ
    local Block = game.ReplicatedStorage.Experimental
    local WhenClicked = game.ReplicatedStorage.WhenClicked.Value
    print('works3')
    while WhenClicked == true do
        print('wore')
        PlacedBlock = Block:Clone()
        PlacedBlock.Parent = workspace
        PlacedBlock.Position.X = ImportedValueX
        PlacedBlock.Position.Y = ImportedValueY
        PlacedBlock.Position.Z = ImportedValueZ
        WhenClicked = false
        wait(1)
    end
    wait(0.1)
end

变量工作得很好,whenclicked部分也可以工作,我认为while whenclicked的部分出了问题。

点赞
用户1296374
用户1296374

我看到一个问题:

PlacedBlock.Position.X = ImportedValueX
PlacedBlock.Position.Y = ImportedValueY
PlacedBlock.Position.Z = ImportedValueZ

X、Y、Z 是只读属性。你需要通过创建一个新的 Vector3 对象并将其分配给 Position 属性来填充它们,如下所示:

PlacedBlock.Position = Vector3.new(ImportedValueX, ImportedValueY, ImportedValueZ)

更新:

我假设您正在尝试使用 ReplicatedStorage 将鼠标单击状态(whenClicked)从客户端发送到服务器。然后,服务器会在循环中检查状态以及 x/y/z 位置。这不起作用,因为 ReplicatedStorage 不会将您的值复制到服务器。否则,这可能会为漏洞打开大门。 因此,为了将某些内容从客户端发送到服务器,您应该使用 RemoteEvent 或 RemoteFunction(在参考手册中查找)。在您的情况下,您的服务器脚本可能如下所示:

local event = Instance.new("RemoteEvent", game.ReplicatedStorage)
event.Name = "MyRemoteEvent"

local Block = game.ReplicatedStorage.Experimental

event.OnServerEvent:Connect(function(plr, position, whenClicked)
    if whenClicked then
        print('wore')
        local placedBlock = Block:Clone()
        placedBlock.Parent = workspace
        placedBlock.Position = position
    end
end)

这将在 ReplicatedStorage 中创建一个远程事件,然后监听它。当它从客户端调用时,它将执行您想要的操作(克隆该部分并定位它)。

在客户端脚本中,您将像这样触发事件:

-- 等待服务器创建远程事件
local event = game.ReplicatedStorage:WaitForChild("MyRemoteEvent")

-- 做任何您需要做的事情,然后调用触发事件:
event:FireServer(Vector3.new(5,5,5), true)
2020-06-06 15:11:48