I made a build script which Invokes 2 RemoteFunctions (PlaceBlock, RemoveBlock) but It has alot of input lag, is there any way to fix this problem?

This is the Full Build Script:

-- Function
local function PlaySound(SoundId)
	local Sound = Instance.new("Sound")
	Sound.Parent = workspace
	Sound.SoundId = SoundId
	Sound:Play()
	Sound.Ended:Wait()
	Sound:Destroy()
end

local function GetBlock()
	local Tool = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Tool")
	return Tool
end

-- Get Player
local Player = game.Players.LocalPlayer
local PlayerData = game.ReplicatedStorage.PlayerData[Player.Name]

-- MainThings
local Room = PlayerData.Build.Value
local PlayerBuild = Room.Build
local TileGrid = Room.Tiles
local Mouse = Player:GetMouse()
local TargetedBlock

-- Border
local Border = Instance.new("SelectionBox")
Border.Parent = workspace
Border.LineThickness = .01
Border.Color3 = Color3.new(255,255,255)

-- Services
local RunService = game:GetService("RunService")

-- Block Data Handler
local BlockSize = Vector3.new(3,3,3)
local BlockMaterial = Enum.Material.Wood
local BlockColor = BrickColor.White()

-- Events
local PlaceBlock = game.ReplicatedStorage.PlaceBlock
local RemoveBlock = game.ReplicatedStorage.RemoveBlock

-- Make Border on Targeted Block
RunService.Heartbeat:Connect(function()
	local Target = Mouse.Target
	if Target then
		if Target.Parent == TileGrid or Target.Parent == PlayerBuild then
			Border.Adornee = Target
			TargetedBlock = Target
		else
			Border.Adornee = nil
			TargetedBlock = nil
		end
	end
end)

local Debounce = true

-- Create Block on Click
Mouse.Button2Down:Connect(function()
	if Debounce == true then
		local Block = GetBlock()
	
	if Block then
	if TargetedBlock then
		Debounce = false
		
		
		-- Up
		if Mouse.TargetSurface == Enum.NormalId.Top then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(0,3,0)), Room)
			
		end
		-- Down
		if Mouse.TargetSurface == Enum.NormalId.Bottom then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(0,-3,0)), Room)
			
		end
		-- Left
		if Mouse.TargetSurface == Enum.NormalId.Left then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(-3,0,0)), Room)
			
		end
		-- Right
		if Mouse.TargetSurface == Enum.NormalId.Right then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(3,0,0)), Room)
			
		end
		-- Front
		if Mouse.TargetSurface == Enum.NormalId.Front then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(0,0,-3)), Room)
			
		end
		-- Back
		if Mouse.TargetSurface == Enum.NormalId.Back then
			PlaceBlock:InvokeServer(Enum.Material[Block.Material.Value], Block.Color.Value, (TargetedBlock.Position + Vector3.new(0,0,3)), Room)
			
		end
		
		wait(0.5)
		Debounce = true
	end
	end
	
	end
end)

-- Remove Block
Mouse.Button1Down:Connect(function()
	if TargetedBlock.Parent == PlayerBuild then
		print("Attempt to remove block")
		RemoveBlock:InvokeServer(TargetedBlock)
	end
end)

Here are the RemoteFunction scripts that will make them work

-- Place Block

local Events = game.ReplicatedStorage

-- Place Block
Events.PlaceBlock.OnServerInvoke = function(player, Mat, Color, Position, room)
    local NewBlock = Instance.new("Part")
    NewBlock.BrickColor = Color
    NewBlock.Material = Mat
    NewBlock.Size = Vector3.new(3,3,3)
    NewBlock.Position = Position
    NewBlock.Parent = room.Build
    NewBlock.Anchored = true
	NewBlock.Name = "Block"
end

-- Remove Block

local Events = game.ReplicatedStorage

-- Remove Block
Events.RemoveBlock.OnServerInvoke = function(Player, TargetedBlock)
	TargetedBlock:Destroy()
end

3 comments

0

Maybe try it with remoteevents

0

@KochbanaemitGuave before i used RemotEvents and they were worse

0

@KochbanaemitGuave before i used RemotEvents and they were worse