Can't spawn an object in the hit position

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)

Try this:

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)

Error:" Position is not a valid member of Model “Workspace.idan1973(myusername)”

-- 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)

See if that helps.

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

It doesn’t work with this code. Do you maybe know how do I change the height with the code valamitas wrote ?

The : indicates type-checking, which allows a programmer to check that class of variable they’re passing around.

In this case, I’m checking whether other_part is of class Instance.Part.

To change position, simply change the pos variable:

local pos : Vector3 = Vector3.new(other_part.Position.X, other_part.Position.Y + 1, other_part.Position.Z)

Hope this helps.

It works but for some reason the new blocks now stuck in the air:

That’s what I mean. Change the positioning of the part on the Y axis by editing the value of pos to suit your needs:

local pos : Vector3 = Vector3.new(other_part.Position.X, other_part.Position.Y + HOW_MUCH_UP, other_part.Position.Z)

But its like real stuck it’s not anchored or anything when it spawn its just stucks.

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 :smiley:

It’s unanchored when it’s in replicatedstorage and also when it’s in the workspace, do you maybe know what could cause the problem ?

Unsure. The only other factors that would influence it would be the gravity of the workspace and whether the part has a velocity or force added to it.

I found the problem it’s because it had weld. Thank you so much for helping!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.