クリックしたところにパーツを置くツールを作成します。
- WorkspaceにToolを追加します。
- Toolの中にPartを入れます。
- Partの名称をHandleに変えます。
- ToolをStarterPackに移動します。
- Toolの中にRemoteEventを追加します。(クライアントからサーバープログラムを実行するため)
- Toolの中にLocalScriptを追加します。(クライアントで動作するプログラム)
- Toolの中にScriptを追加します。(サーバーで動作するプログラム)
- LocalScriptとScriptに次のプログラムを入れます。
LocalScriptのプログラム
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = script.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")
local REACH = 100 -- どのくらい先までクリックが有効か
function OnEquipped(mouse)
mouse.Button1Down:Connect(function()
local unitRay = mouse.UnitRay
local ray = Ray.new(unitRay.Origin, unitRay.Direction * REACH)
local hit, pos, normal = game.Workspace:FindPartOnRay(ray, player.Character)
if hit then
remoteEvent:FireServer(pos)
wait(0.5)
end
end)
end
tool.Equipped:connect(OnEquipped)
Scriptのプログラム
local tool = script.Parent
local remoteEvent = tool:WaitForChild("RemoteEvent")
function putBlock(player,position)
local part = Instance.new("Part")
part.Size = Vector3.new(3,3,3)
part.Position = position
part.Parent = game.Workspace
end
remoteEvent.OnServerEvent:Connect(putBlock)
RemoteEventも使わずに、LocalScript上でputBlockを行うと、クライアントだけが見えるパーツが生成できます。