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)
local part = --part location part.CFrame = CFrame.new(workspace:WaitForChild(player.Name).HumanoidRootPart.X, part.CFrame.Y, part.CFrame.Z)
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:
client
)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:
Position
of the part to mouse.Hit.Position
, aka mousePos
.I personally think that you may have overcomplicated this process.