Placing Parts from Client Side to Server Side Using Mouse Position

I’ve been currently delving into Remote Events and I’ve been struggling on how to make my code work.
What I am trying to aim for is to place parts in a loop from the server side while activating the loop and sending the mouse position from the client side. The problem is that the parts are not placing and I have been struggling for the past 3 hours.

Here are the scripts below:

CLIENT SIDE (LocalScript) ~~ In StarterPlayerScripts

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouse = player:GetMouse()
local mouseRemoteEvent = ReplicatedStorage:WaitForChild("MousePosition")
local checkRemoteEvent = ReplicatedStorage:WaitForChild("OnOrOff")
local thefalse = false
local mousePosition = nil

while thefalse and task.wait(0.1) do
	mousePosition = mouse.Hit.Position
	thefalse = thefalse
	mouseRemoteEvent:FireServer(mousePosition)
	checkRemoteEvent:FireServer(thefalse)
end


mouse.Button1Down:Connect(function()
	thefalse = true
end)
mouse.Button1Up:Connect(function()
	thefalse = false
	checkRemoteEvent:FireServer(thefalse)
end)

SERVER SIDE (Script) ~~ In ServerScriptStorage

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouseRemoteEvent = ReplicatedStorage:WaitForChild("MousePosition")
local checkRemoteEvent = ReplicatedStorage:WaitForChild("OnOrOff")
local mousePosition = nil
local thefalse = false

while thefalse and task.wait(0.1) do
	local part = Instance.new("Part")
	part.Size = Vector3.new(10,10,10)
	part.Shape = Enum.PartType.Cylinder
	part.Anchored = false
	part.Material = "Plastic"
	part.BrickColor = BrickColor.new("Bright blue")
	part.Position = mousePosition
	part.Parent = workspace
	task.delay(5, function() part:Destroy() end)
end


mouseRemoteEvent.OnServerEvent:Connect(function (receivedNumber)
	mousePosition = receivedNumber
end)

checkRemoteEvent.OnServerEvent:Connect(function (receivedBoolean)
	thefalse = receivedBoolean
end)

Anyone got any ideas to why the script isn’t working?

1 Like

Within the while loop, you are telling it to fire if thefalse is true, instead of false, if you say if boolean then, the if statement will expect the boolean to the truthy (meaning true), and will only fire if it is true, if you say if not boolean then, it will expect it to be falsy (not true, or nil), because the while loop is expecting a truthy value, and not a falsy value, it will not run the following loop, so you need to specify what you want it to be false by saying not thefalse, thefalse == false, or by swapping how they work

Debris exists for a reason.


Edit: another thing, OnServerEvent will return the Player first, and then the values given, so you may need to add another parameter.

I changed the Server Side Script

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouseRemoteEvent = ReplicatedStorage:WaitForChild("MousePosition")
local checkRemoteEvent = ReplicatedStorage:WaitForChild("OnOrOff")
local thefalse = false

local mousePosition

function summon()
while thefalse and task.wait(0.1) do
	local part = Instance.new("Part")
	part.Size = Vector3.new(10,10,10)
	part.Shape = Enum.PartType.Cylinder
	part.Anchored = false
	part.Material = "Plastic"
	part.BrickColor = BrickColor.new("Bright blue")
	part.Position = Vector3.new(mousePosition)
	print(mousePosition)	
	part.Parent = workspace
	task.delay(5, function() part:Destroy() end)
	end
end

mouseRemoteEvent.OnServerEvent:Connect(function (player, receivedNumber)
	mousePosition = receivedNumber
	summon()
end)

checkRemoteEvent.OnServerEvent:Connect(function (player, receivedBoolean)
	thefalse = receivedBoolean
end)

Now it’s spawning blocks, and the MousePosition is updating since I checked it with print. So the only problem left is the vector3.new which I have no clue how to write it in code

Solved it, I just added a function in my Server Side and Client Side, and I also added the Vector3.new to the mouse Position. Then I added 3 new variables to both client and server side, Position X, Y and Z. I also added a Stop and Start Button on the GUI Client Side that uses a NumberValue which I used in a local variable called Check. I put the NumberValue in game.StarterGui.ScreenGui.Frame.Check. I removed the loop in the Server Side Script also.

Here’s the new code below:

CLIENT SIDE (Start Script)

local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouse = player:GetMouse()
local mouseRemoteEvent = ReplicatedStorage:WaitForChild("MousePosition")
local thefalse = false
local mouseHit = nil
local mousePositionX = nil
local mousePositionY = nil
local mousePositionZ = nil
local checkForGUI = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui")
local check = checkForGUI.Frame.Check

function start()
	if (check.Value == 0) then
		while thefalse and task.wait(0.2) do
			mouseHit = mouse.Hit.Position
			mousePositionX = mouseHit.X
			mousePositionY = mouseHit.Y
			mousePositionZ = mouseHit.Z
			mouseRemoteEvent:FireServer(mousePositionX, mousePositionY, mousePositionZ)
		end
	end
end

mouse.Button1Down:Connect(function()
	thefalse = true
	start()
end)
mouse.Button1Up:Connect(function()
	thefalse = false
end)

CLIENT SIDE (On and Off Script) (GUI Text Button)

local button = script.Parent
local CheckForSwitch = true
local Check = button.Parent.Check

button.MouseButton1Click:Connect(function()
	if (CheckForSwitch) then
		CheckForSwitch = false
		script.Parent.Text = "Off"
		button.BackgroundColor3 = Color3.new(1, 100/255, 100/255)
		Check.Value = 1
	else
		CheckForSwitch = true
		script.Parent.Text = "On"
		button.BackgroundColor3 = Color3.new(61/255, 255/255, 110/255)
		Check.Value = 0
	end
end)

SERVER SIDE (Summon Script)

local Debris = game:GetService("Debris")
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mouseRemoteEvent = ReplicatedStorage:WaitForChild("MousePosition")

function summon(player, mousePositionX, mousePositionY, mousePositionZ)
		local part = Instance.new("Part")
		part.Size = Vector3.new(10,10,10)
		part.Shape = Enum.PartType.Cylinder
		part.Anchored = false
		part.Material = "Plastic"
		part.BrickColor = BrickColor.new("Bright blue")
		part.Position = Vector3.new(mousePositionX, mousePositionY, mousePositionZ)
		part.Parent = workspace
		Debris:AddItem(part, 8)
		wait(5)
		part.CanCollide = false
end

mouseRemoteEvent.OnServerEvent:Connect(summon)

This code works for me, and that’s enough. If you don’t want it to loop, go to the Client Side Start Script and remove the while loop. Then get rid the of the local variable thefalse and remove it also in the mouse:connect. After that, you can delete mouse.Button1Up and change mouse.Button1Down to mouse.Button1Click

1 Like

Updated the Script to make it more cleaner and efficient, updated script is above.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.