Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to create a block appear front humanoidrootpart ?

Asked by 4 years ago
Edited 4 years ago

I tried to write a script when the player click mouse, a block would appear in front of the player but it didn't work. When the player turns away, the block does not appear in front of the player anymore. i mean the block still appears but Its position is not as I would like

game.ReplicatedStorage.Events.Block.OnServerEvent:Connect(function(player, mousePos)
    local f = game.ReplicatedStorage.BlockStore
    local block = f.Block:Clone()
        local character = player.Character


    block.Rotation = character.HumanoidRootPart.Rotation + Vector3.new(0,-180.55,0)
    block.Position = character.HumanoidRootPart.Position + Vector3.new(5,0,5)
    block.Parent = workspace

end)
0
please give a script NSMascot 113 — 4y
0
i was edit tieuvuong07 -8 — 4y

2 answers

Log in to vote
0
Answered by
NSMascot 113
4 years ago
Edited 4 years ago
local part = --part location

part.CFrame = CFrame.new(workspace:WaitForChild(player.Name).HumanoidRootPart.X, part.CFrame.Y, part.CFrame.Z)
0
no, i can click mouse, i work, but the block can not appear front of humanoid, when player turn away, the block drop behind or left or right player tieuvuong07 -8 — 4y
0
i changed my code, try this? NSMascot 113 — 4y
0
it's not work tieuvuong07 -8 — 4y
0
try googling the answer NSMascot 113 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You're not even using the mousePos parameter that you put into the function. That's the problem.

I'm going to assume that mousePos's argument is the position of the player's Mouse at the time that they had clicked Button1 (left mouse button). Here's a really simple example of spawning a part where the player clicks:

Local

local event = game:GetService("ReplicatedStorage"):WaitForChild("MouseEvent")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
    event:FireServer(mouse.Hit.Position)
end)

Server

local event = game:GetService("ReplicatedStorage"):WaitForChild("MouseEvent")
event.OnServerEvent:Connect(function(client, mousePos)
    if client then -- Fail safe; this is to check if the player is still in-game
        local f = game:GetService("ReplicatedStorage"):WaitForChild("BlockStore")
        local block = f.Block:Clone()
        block.Position = mousePos
        block.Parent = workspace
    end
end)

Now for the explanation.

Button1Down is an event of the player's Mouse object that fires whenever the player clicks their left mouse button. mouse.Hit is the CFrame of the mouse that represents the 3D space that the mouse clicked on. When the RemoteEvent named MouseEvent fires the OnServerEvent script signal, it has to pass two arguments to its given parameters:

  • The client (player) that fired the event (represented by client)
  • A tuple of arguments that was included within the parenthesis of FireServer(). If no 2nd, 3rd, etc. parameter is defined for the function to use, this argument is not assigned. The same happens if nothing is put in FireServer()'s parenthesis when the function is called.

When the signal fires, the script checks to see if client exists (meaning that the player associated with said client is still in-game). If it passes the fail safe, the script will clone the child of BlockStore, being Block, and do two things:

  • Assign the Position of the part to mouse.Hit.Position, aka mousePos.
  • Parent the clone to Workspace.

I personally think that you may have overcomplicated this process.

Answer this question