Spawned part spawns on a permanent side, and does not change where to spawn when the orientation is changed

Hello!

Thank you if you came from my previous scripting support post, another bug occurs right when I fix the 1st one that I don’t know how to fix! Basically, I have a spawn system that spawns a part that is supposed to always spawn at the back of the model, however, if the model changed direction, It does not change with it, and continues to spawn in the same exact location it did before. Is there a solution to this? Here is the code.

local maxwell = script.Parent.Character.maxwell
local ClickDetector = script.Parent.Hitbox.ClickDetector

local replicatedStorage = game:GetService("ReplicatedStorage")
local cash = replicatedStorage:WaitForChild("cash")

local canRun = true
ClickDetector.MouseClick:Connect(function()
	if canRun == true then
		local newCash = cash:Clone()
		local maxwellPosition = maxwell.Position
		local cashPosition = maxwellPosition + Vector3.new(4,0,0)
		newCash.Position = cashPosition
		newCash.Parent = workspace
		canRun = false
		wait(0.2)
		canRun = true
	end
end)

If you are able to help out that would be much appreciated!

Try moving in local space with a CFrame transformation:

newCash.CFrame = maxwell.CFrame * CFrame.new(0, 0, -4)

You might need to toy with the offset value.

You have to use CFrame for this.
So it would look something like this:

local maxwellCFrame = maxwell.CFrame -- CFrame is the position, with orientation
local cashCFrame = maxwellCFrame * CFrame.new(0, 0, -4)
newCash.CFrame = cashCFrame

It is basically the script which @ElusiveEpix uses :sweat_smile:

1 Like

It works very well, however, if it is moving and I click it it will not spawn, only when it stands still. Any fixes for that?

Nevermind this, apparently, it was collecting its own cash which is why it would not spawn and instantly be picked up. Lol. Thank’s a lot!

1 Like

No worries man, I am here to help! If you find any other issues dont be shy to reach out! :smile:

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