Hey, so i’m trying to make a small merge game I don’tunderstand how to set the position of the new block that spawns after to similar blocks hit each other. I made a code that findes the collision position but i don’t know what do I need to type in the vector3(). help wouldbe aprriciated!
Here is the script:
local part = script.Parent
local function destroy(otherPart) -- touch function
if otherPart.Name == "FirstBlock" then -- if the blocks are the same block level they will be destroyed
script.Parent.Touched:Connect(function(hit)
print("touched at".. tostring(script.Parent.Position))
print("touched part's position".. tostring(hit.Position))
part:Destroy()
otherPart:Destroy()
print("destroyed")
local secondblock = game.ReplicatedStorage.SecondBlock -- spawns the next level block
local secondblockcopy = secondblock:Clone()
secondblockcopy.Position = Vector3(hit.Position) -- Problem
secondblockcopy.Name = "SecondBlock"
secondblockcopy.Parent = workspace
end)
end
end
part.Touched:Connect(destroy)
local part = script.Parent
local function destroy(hit) -- touch function
if hit.Parent.Name == "FirstBlock" then -- if the blocks are the same block level they will be destroyed
script.Parent.Touched:Connect(function(hit)
print("touched at".. tostring(script.Parent.Position))
print("touched part's position".. tostring(hit.Parent.Position))
part:Destroy()
otherPart:Destroy()
print("destroyed")
local secondblock = game.ReplicatedStorage.SecondBlock -- spawns the next level block
local secondblockcopy = secondblock:Clone()
secondblockcopy.Position = Vector3(hit.Parent.Position) -- Problem
secondblockcopy.Name = "SecondBlock"
secondblockcopy.Parent = workspace
end)
end
end
part.Touched:Connect(destroy)
-- Handles organising parts
local function main(other_part : Part)
-- Equality checks the name of the part hitting the script's parent
if other_part.Name == "FirstBlock" then
-- Saves the position of the part hitting the script's parent for later
local pos : Vector3 = other_part.Position
-- Deletes the other part
other_part:Destroy()
-- Clones your new part and changes properties
local second_block : Part = RS:WaitForChild("SecondBlock"):Clone()
second_block.Parent = game.Workspace
-- New part's position is the position of the old part
second_block.Position = pos
-- Deletes the script's parent part, which also terminates the script
script.Parent:Destroy()
end
end
-- Connects the script's parent's Touched event to the above function
script.Parent.Touched:Connect(main)
It works! But now the new block stuck in the ground do you maybe know how to make it spawn higher? And also can you pls explain to me (what other_part : Part) means
My code simply clones the part you have in ReplicatedStorage. In order to make it fall, you need to edit the properties of the part in ReplicatedStorage. Ensure that the part is unanchored there first