Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

(please i'm desperate) Why is the NPC I am trying to place offset?

I am trying to make it so that you can place an NPC. It all works percectly, except where it is placed. The excact location is correct, but where the NPC is placed is wrong. PLEASE help me I am desperate. I asked several times with no response.

CODE:

local function place()
	print("func fired")
	local newTower = tower:Clone()
	local bubble = newTower.Bubble
	local range = newTower.Settings.Range.Value
	local account = 16
	bubble.Size = Vector3.new(0.5, range + account, range + account)
	bubble.Transparency = 0.6
	bubble.Material = Enum.Material.Neon
	local yOrientation = 0
	local canPlace = false
	if not placing then
		placing = true
		newTower.Parent = workspace
		game:GetService("RunService").RenderStepped:Connect(function()
			if newTower and placing then
				local mouseRay = mouse.UnitRay
				local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000000000)
				local ignoreList = newTower:GetChildren()
				table.insert(ignoreList, #ignoreList, root.Parent)
				local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

				if hit then
					if hit.Name == "Grass" and hit.Parent.Name == "FloorTowers" then
						canPlace = true
						bubble.Color = Color3.fromRGB(0, 160, 255)
					else
						canPlace = false
						bubble.Color = Color3.fromRGB(255, 0, 0)
					end
					if newTower:FindFirstChild("Bubble") then
						newTower.Bubble.Transparency = 0.6
					end
					local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
					local newCFrame = CFrame.new(position.X, position.Y + yOffset, position.Z)
					if newTower then
						if newTower.PrimaryPart then
							newTower.PrimaryPart.CFrame = newCFrame * newCFrame
						end
					end
				end
			end
		end)
		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.R then
				rKeyDown = true
				local speed = 2.5
				while rKeyDown do
					yOrientation = yOrientation + speed
					tower.PrimaryPart.Orientation = Vector3.new(0, yOrientation, 0)
					wait()
				end
			end
		end)
		uis.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.R then
				rKeyDown = false
			end
		end)
		mouse.Button1Down:Connect(function()
			if placing then
				if canPlace and newTower then
					game.ReplicatedStorage.Remotes.PlaceTower:FireServer(tower, newTower.PrimaryPart.CFrame)
					game.ReplicatedStorage.Remotes.PlaceTower.OnClientEvent:Wait()
					newTower:Destroy()
					placing = false
				end
			end
		end)
		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Z then
				placing = false
				newTower:Destroy()
			end
		end)
	end
end

TYSM!

7 comments

1

Instead of setting the actual parts cframe as the towers cframe when you fire the remote, keep a local variable that uses the same CFrames as what you set the temporary tower's cframe as. If its the way you want prior to placing it, then I would send those CFrame cords to the server, not the temporary towers cframe.

1

e.g: game.ReplicatedStorage.Remotes.PlaceTower:FireServer(newCFrame * newCFrame)

0

I don't know what you mean

0

Okay I got the remote event, and I realize how that is better, but I don't understand the other part. Please elaborate. Thanks for your help btw! :D

0

also ninja I did what you said and it still didn't fix it. the tower is still ofset

1

I posted an answer, I don't know if it'll fix it our not. I have a few errands to run IRL and I'll check back after and see if we can get a solution if it doesnt work.

0

kk

1 answer

--Not sure if this would work but if it sets the temporary towers cframe correctly, then it should.
local TowerCFrame -- Define the CFrame variable to fire the remote with

local function place()
	print("func fired")
	local newTower = tower:Clone()
	local bubble = newTower.Bubble
	local range = newTower.Settings.Range.Value
	local account = 16
	bubble.Size = Vector3.new(0.5, range + account, range + account)
	bubble.Transparency = 0.6
	bubble.Material = Enum.Material.Neon
	local yOrientation = 0
	local canPlace = false
	if not placing then
		placing = true
		newTower.Parent = workspace
		game:GetService("RunService").RenderStepped:Connect(function()
			if newTower and placing then
				local mouseRay = mouse.UnitRay
				local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000000000)
				local ignoreList = newTower:GetChildren()
				table.insert(ignoreList, #ignoreList, root.Parent)
				local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

				if hit then
					if hit.Name == "Grass" and hit.Parent.Name == "FloorTowers" then
						canPlace = true
						bubble.Color = Color3.fromRGB(0, 160, 255)
					else
						canPlace = false
						bubble.Color = Color3.fromRGB(255, 0, 0)
					end
					if newTower:FindFirstChild("Bubble") then
						newTower.Bubble.Transparency = 0.6
					end
					local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
					local newCFrame = CFrame.new(position.X, position.Y + yOffset, position.Z)
					if newTower then
						if newTower.PrimaryPart then
							newTower.PrimaryPart.CFrame = newCFrame * newCFrame
							TowerCFrame = newCFrame * newCFrame -- Set our defined variable to this cframe
						end
					end
				end
			end
		end)
		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.R then
				rKeyDown = true
				local speed = 2.5
				while rKeyDown do
					yOrientation = yOrientation + speed
					tower.PrimaryPart.Orientation = Vector3.new(0, yOrientation, 0)
					wait()
				end
			end
		end)
		uis.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.R then
				rKeyDown = false
			end
		end)
		mouse.Button1Down:Connect(function()
			if placing then
				if canPlace and newTower then
					game.ReplicatedStorage.Remotes.PlaceTower:FireServer(tower, TowerCFrame) -- Fire the remote with our defined variables cframe.
					game.ReplicatedStorage.Remotes.PlaceTower.OnClientEvent:Wait()
					newTower:Destroy()
					placing = false
				end
			end
		end)
		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Z then
				placing = false
				newTower:Destroy()
			end
		end)
	end
end

4 comments

0

yeah, it's still broken. I will get the game link for you to check out how it's broken

0

everything works, it's just that the tower is slightly offset

0

I am going to get something to eat, thanks for the help btw!

This discussion is preserved as it was. New answers, comments, and votes are closed.