self.Preview won't change

Can you add print here?

print("e")
local objectName = item:GetAttribute("ItemName")
print(objectName)
clientPlacer:ChangeObject(player, objectName)
4 Likes

I will try so, i’ll edit this post once i do so

1 Like

image
So that script is fine. It has to be the other 2 snippets.

2 Likes

The only other thing I can think of is second self in

self:PreparePreviewModel(objects:FindFirstChild(self.SelectedObject))

does not reference ClientPlacer. Try replacing with objectName

self:PreparePreviewModel(objects:FindFirstChild(objectName))
4 Likes

Nope, same problem, it does not fix it.

bump once again once again

1 Like

Okay, I’ve seen all of your bumps now and I just have to respond I guess:

I don’t understand how I am supposed to help?
In the guidelines for “Scripting Support” it states that you have to be able to answer these 3 questions:

  • What do you want to achieve
  • What seems to be the issue
  • What have you tried so far to fix it

You want your system fixed but you did not explain where the issue most likely lies. I do not think many people want to go around searching for issues that might be outside of their scope →

  • Is this the only code related to the area of problem occurance?
  • What have you already tried to fix the issue?
  • Where exactly is the issue? What does not work and where is the code related to it?

I am here to help, so please answer the questions and stop bumping this post.

2 Likes

So basically, i’ve ruled out that these two snippets hold the issue:


-- Client placer pt.1 (renders the object that moves wherever my mouse points at)

function ClientPlacer:PreparePreviewModel(model: Model)
	if self.Preview then 
		self.Preview:Destroy()
	end

	self.Preview = model:Clone()
	local boxOutline = boxOutlineTemplate:Clone()
	boxOutline.Adornee = self.Preview 
	boxOutline.Parent = self.Preview

	for _, part in self.Preview:GetDescendants() do
		if part:IsA("BasePart") then
			part.CanCollide = false
			part.CanQuery = false
			part.Transparency = 0.5
		end
	end

	self.Preview.Parent = workspace
end

-- Client placer pt.2 (where the object value is changed)
   function ClientPlacer:ChangeObject(player: Player, objectName: string)
	local objects = ReplicatedStorage:WaitForChild("Objects")
	self.SelectedObject = objectName
	
	self:PreparePreviewModel(objects:FindFirstChild(self.SelectedObject))
end

Adding on, i want it so where you select a new item, the previous one destroys and now follows my mouse, but it does not.

Ive tried discord servers, chatgpt but failed.

4 Likes

I’m sending this early in the morning, but where is your “self” set in the script? And what is it set to?

2 Likes

This is a module script, self does not need to be initialized

3 Likes

Well i’ve initialized it as so:


function ClientPlacer.new(plot: Model)
	local self = setmetatable(
	{Plot = plot, 
	Preview = nil,
	SelectedObject = "Conveyor",
	Rotation = 0, 
	}, ClientPlacer)
	
	self:InitRenderPreview()
	
	ContextActionService:BindAction(PLACE_ACTION,
		function(...) self:TryPlaceBlock(...) end,
		false,
		Enum.UserInputType.MouseButton1)
	ContextActionService:BindAction(ROTATE_ACTION,
		function(...) self:RotateBlock(...) end,
		false,
		Enum.KeyCode.R
	)
	ContextActionService:BindAction(DELETE_ACTION,
		function(...) self:TryDeleteBlock(...) end,
		false,
		Enum.KeyCode.X
	)
	return self
end

@haydengamingdo

3 Likes

Are you re-running the :PreparePreview once the SelectedObject is changed?

2 Likes

Yes i am changing it.

101010101001010111011

2 Likes

Can I see the script snippet that moves the preview block?

2 Likes
function ClientPlacer:RenderPreview()
	local cast = castMouse()
	if cast and cast.Position then
		local cf = CFrame.new(cast.Position) * CFrame.Angles(0, self.Rotation, 0)
		self.Preview:PivotTo(cf)
		print(self.Preview)
		
		local size = self.Preview:GetExtentsSize()
		self.Preview.BoxOutline.Color3 = 
			if placementValidator.WithinBounds(self.Plot, size, cf) and 
			placementValidator.NotIntersectingObject(self.Plot, size, cf) then 
				Color3.new(0, 1, 1)
			else
				Color3.new(1, 0, 0)
	end
end
2 Likes

Make sure you check if the self.Preview exists before attempting to use :PivotTo()!

Edit: If rotation does not work, use math.rad(self.Rotation)

2 Likes

Okay, i tried it out and self.Preview exists before:PivotTo() but it’s not the right one, it should be ‘ConveyorTurn’ but it’s still ‘Conveyor’

2 Likes

Try re- running the :PreparePreviewModel function at the start of the :RenderPreview func

2 Likes

That just broke it even more lol.

Hang on: RenderPreview is not the problem, PrepareRenderPreview is.

2 Likes

How did it break in which way? Was it like a duplication issue or just a lag issue?

2 Likes

Is the :PrepareRenderPreview the :PreparePreviewModel func?

2 Likes