Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made a script so that when the player clicks the player will place a block?

I made a local script

-- Get Player
local Player = game.Players.LocalPlayer

-- MainThings
local TileGrid = workspace.Build1.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 then
			Border.Adornee = Target
			TargetedBlock = Target
		else
			Border.Adornee = nil
			TargetedBlock = nil
		end
	end
end)

-- Create Block on Click
Mouse.Button1Down:Connect(function()
	if TargetedBlock then
		if Mouse.TargetSurface == Enum.NormalId.Top then
			print("Attempt to place block")
			PlaceBlock:InvokeServer(Player, BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,3,0)))
		end
	end
end)

Earlier I made a function inside the script so that it will place a block, but after that I realised it was only affecting the client so I made Remote a Remote Function.

here is the script that makes the remote function work

local Events = script.Parent

Events.PlaceBlock.OnServerInvoke:Connect(function(Player, Mat, Color, Position)
	print("Block Is Placed")
	local NewBlock = Instance.new("Part")
	NewBlock.BrickColor = Color
	NewBlock.Material = Mat
	NewBlock.Size = Vector3.new(3,3,3)
	NewBlock.Position = Position
	NewBlock.Parent = workspace.Build1
	NewBlock.Anchored = true
end)

After I tried this, it wasn't working... The local script is doing its part but when it Invokes the script is not printing the message i made it print

1 answer

✓ Accepted answer

So Try This For The Local Script:

-- Get Player
local Player = game.Players.LocalPlayer

-- MainThings
local TileGrid = workspace.Build1.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 then
            Border.Adornee = Target
            TargetedBlock = Target
        else
            Border.Adornee = nil
            TargetedBlock = nil
        end
    end
end)

-- Create Block on Click
Mouse.Button1Down:Connect(function()
    if TargetedBlock then
        if Mouse.TargetSurface == Enum.NormalId.Top then
            print("Attempt to place block")
            PlaceBlock:InvokeServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,3,0)))
        end
    end
end)

You Do Not Have To Send The Player Value As It Automatically Sent With The Extra Data.

If That Doesn't Work Then Instead Of Using A Remote Function Use A Remote Event And Then All You Have To Do Is Use The Scripts Below:

Local Script:

-- Get Player
local Player = game.Players.LocalPlayer

-- MainThings
local TileGrid = workspace.Build1.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 then
            Border.Adornee = Target
            TargetedBlock = Target
        else
            Border.Adornee = nil
            TargetedBlock = nil
        end
    end
end)

-- Create Block on Click
Mouse.Button1Down:Connect(function()
    if TargetedBlock then
        if Mouse.TargetSurface == Enum.NormalId.Top then
            print("Attempt to place block")
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,3,0)))
        end
    end
end)

The Server Script:

local Events = script.Parent

Events.PlaceBlock.OnServerEvent:Connect(function(player, Mat, Color, Position)
    print("Block Is Placed!")
    local NewBlock = Instance.new("Part")
    NewBlock.BrickColor = Color
    NewBlock.Material = Mat
    NewBlock.Size = Vector3.new(3,3,3)
    NewBlock.Position = Position
    NewBlock.Parent = workspace.Build1
    NewBlock.Anchored = true
end)

Also Try Putting The Server Script In ServerScriptService And Putting The Remote In Replicated Storage

Hope That This Helps!

2 comments

0

Thanks! All I had to do was to put the Script in to the ServerScriptService

0

No Problem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.