Hi there! I’m trying to make a script that if a part is touching another part it gets destroyed. The way my placement system for my game works is the block you place turns into your name. So I want the script to check the list of players and if the part matches a name of a player in-game it gets destroyed. I’ve tried making a script for it but it doesn’t work.
Here is the script so far:
local Part = script.Parent
local function DestroyBlock(TouchingPart)
TouchingPart:Destroy()
end
local function HandleTouch(TouchPart)
-- Check if the touching part is script.Parent
if TouchPart == Part then
-- Get the username from the part name
local username = TouchPart.Name
-- Check if the username matches any player's username
for _, player in ipairs(game.Players:GetPlayers()) do
if username == player.Name then
-- Destroy the block
DestroyBlock(TouchPart)
break
end
end
end
end
Part.Touched:Connect(HandleTouch)
GetTouchingParts, but I’m pretty sure that’s not your issue. The script doesn’t really look wrong.
By any chance, does your placement system use CFrame to determine the position of the block? CFrame doesn’t update touched contacts - Touched will only fire as a result of physics causing two parts to intersect or collide with each other. CFrame will not fire touched.
It’s kind of an outdated system, but here are the scripts.
ServerSided script:
script.Parent.AddBlock.OnServerEvent:Connect(function(plr, pos)
local btp = game.ReplicatedStorage.Block_Wood:Clone()
btp.Parent = workspace
btp.CanCollide = true
btp.Position = pos
btp.Name = plr.Name
end)
script.Parent.RemoveBlock.OnServerEvent:Connect(function(plr, block)
if block.Name == plr.Name then
block:Destroy()
end
end)
LocalScript:
local mouse = game.Players.LocalPlayer:GetMouse()
local off = false
function round(vector, unit)
return vector - Vector3.new(vector.X%unit, vector.Y%unit, vector.Z%unit)
end
script.Parent.Equipped:Connect(function(toolMouse)
if mouse ~= nil then
local clonedBrick = game.ReplicatedStorage.Block_Wood:Clone()
mouse.TargetFilter = clonedBrick
clonedBrick.Transparency = .5
clonedBrick.CanCollide = false
clonedBrick.Parent = workspace
while mouse ~= nil do
local posit2 = round(mouse.Hit.p,2)
clonedBrick.Position = posit2 + Vector3.new(0, 2, 0)
wait()
if off then
clonedBrick:Destroy()
off = false
break
end
end
end
end)
script.Parent.Activated:Connect(function()
local BrickToPlace = game.ReplicatedStorage.Block_Wood:Clone()
local posit = round(mouse.Hit.p,2)
local pos = posit + Vector3.new(0, 2, 0)
script.Parent.AddBlock:FireServer(pos)
end)
script.Parent.Unequipped:Connect(function()
off = true
end)