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

My delete block bindable event is not working when i fire it?

Asked by 3 years ago

I made a script that will destroy blocks when clicked on them I made it in a local script and it only affects the client then I made a bindable event to make it affect the server

Here is the LocalScript

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

-- MainThings
local PlayerBuild = workspace.Build1.Build
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 or PlayerBuild 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

        -- Up
        if Mouse.TargetSurface == Enum.NormalId.Top then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,3,0)))
            print("Attempt to place block")
        end
        -- Down
        if Mouse.TargetSurface == Enum.NormalId.Bottom then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,-3,0)))
            print("Attempt to place block")
        end
        -- Left
        if Mouse.TargetSurface == Enum.NormalId.Left then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(-3,0,0)))
            print("Attempt to place block")
        end
        -- Right
        if Mouse.TargetSurface == Enum.NormalId.Right then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(3,0,0)))
            print("Attempt to place block")
        end
        -- Front
        if Mouse.TargetSurface == Enum.NormalId.Front then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,0,-3)))
            print("Attempt to place block")
        end
        -- Back
        if Mouse.TargetSurface == Enum.NormalId.Back then
            PlaceBlock:FireServer(BlockMaterial, BlockColor, (TargetedBlock.Position + Vector3.new(0,3,3)))
            print("Attempt to place block")
        end
    end
end)

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

The RemoveBlock part is in Line 80 Here is that part

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

And here is the Event script

local Events = game.ReplicatedStorage

-- Place Block
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.Build
    NewBlock.Anchored = true
    NewBlock.Name = "Block"
end)

-- Remove Block
Events.RemoveBlock.OnServerEvent:Connect(function(Block)
    print("Block is removed")
    Block:Destroy()
end)

When I test it deletes the character for some reason, please help to fix this

0
You can't use a BindableEvent for this. What you want is a RemoteEvent. https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events AntiWorldliness 868 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

Debugged this script with Nathan on the SH community chat. Posting the solution for those who come across this and may need it.

-- Remove Block
Events.RemoveBlock.OnServerEvent:Connect(function(Block) -- the first function parameter of OnServerEvent is the player. So defining just one parameter would define the player only. In this case block refers to the player, which is not what we want. 
    print("Block is removed")
    Block:Destroy()
end)
-- Remove Block
Events.RemoveBlock.OnServerEvent:Connect(function(plr, Block)-- adding plr, Block defines a second parameter which should be the variable parameter from the FireServer in the local script
    print("Block is removed")
    Block:Destroy()
end)
Ad

Answer this question