📜  远程事件客户端到服务器 lua - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:17.336000             🧑  作者: Mango

代码示例1
-- Example on creating a part. Using Remote Events
-- Client - Server.

-- (Local Script) Fire Remote Event

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
     
    -- Fire the remote event
    remoteEvent:FireServer()
    
    -- Include additional data when firing the event
    -- Like passing Color and Position to Server
    
    -- remoteEvent:FireServer(BrickColor.Red(), Vector3.new(0, 25, 0)) --

-- (Script) Create Part on Server
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    --Get removeEvent from the ReplicatedStorage
    local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
     
    -- Create a new part
    local function onCreatePart(player, partColor, partPos)
        print(player.Name .. " fired the remote event")
        local newPart = Instance.new("Part")
        
        -- Use attributes you passed on
        newPart.BrickColor = partColor
        newPart.Position = partPos
        newPart.Parent = workspace
    end
     
    -- Call "onCreatePart()" when the client fires the remote event
    remoteEvent.OnServerEvent:Connect(onCreatePart)