Code Issues - Position model to player torso and destroy model property

Hello! I’m trying to position the model on my player’s torso (like a guitar guy) how can I do that? currently it only appears in the workspace, I also need to make sure that the model is destroyed, currently this does not work and it stays there… what can I do to correct these steps?

image

serverscriptservice script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEventAddGuitar = replicatedStorage:WaitForChild("remoteEventAddGuitar")
local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")

-- Function to add the guitar model
local function onAddGuitar(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humRootPart = character:WaitForChild("HumanoidRootPart")

	local guitarCloned = replicatedStorage:WaitForChild("TestingGuitar"):Clone()

	guitarCloned.Parent = humRootPart
	guitarCloned.PrimaryPart.CFrame = humRootPart.CFrame

	if guitarCloned.Parent ~= humRootPart then
		print("Error: Failed to parent model 'TestingGuitar' to HumanoidRootPart.")
	end
end

-- Function to remove the guitar model
local function onRemoveGuitar(player)
	local character = player.Character or player.CharacterAdded:Wait()

	-- Find all model instances with the correct name
	for _, child in pairs(character:GetChildren()) do
		if child.Name == "TestingGuitar" and child:IsA("Model") then
			child:Destroy()
		end
	end
end


-- Connect events to functions
remoteEventAddGuitar.OnServerEvent:Connect(onAddGuitar)
remoteEventRemoveGuitar.OnServerEvent:Connect(onRemoveGuitar)

localscript:

local chooseElectricGuitar = script.Parent.Frame:WaitForChild("On/Off")
local isOn = false
local debounce = false

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEventAddGuitar = replicatedStorage:WaitForChild("remoteEventAddGuitar")
local remoteEventRemoveGuitar = replicatedStorage:WaitForChild("remoteEventRemoveGuitar")

chooseElectricGuitar.MouseButton1Click:Connect(function()
	if not debounce then
		debounce = true

		if isOn then
			-- Turn off the guitar
			print("Turning off...")

			-- Fire the remote event to remove the guitar
			print("Firing remote event to remove the guitar")
			remoteEventRemoveGuitar:FireServer()
		else
			-- Turn on the guitar
			print("Turning on...")

			-- Fire the remote event to add the guitar
			print("Firing remote event to add the guitar")
			remoteEventAddGuitar:FireServer()
		end

		chooseElectricGuitar.Text = isOn and "Off" or "On"
		isOn = not isOn
		task.wait(0.5)
		debounce = false
	end
end)
1 Like

you can use attachments or use assesory. for attachment, use a test rig and add an attachment to the upper torso . shift the attachment to the desired position and copy the offset. Then, make it so that when player spawn in , an attachment get added to the torso with the same offset that I copied. after that , clone the guitar and cframe it to the attachment world cframe . it the guitar orientation is goofy, u can change the orientation in the script