MSG Command to remove model doesn't work

Hello, I’ve been working on some commands (for example: %spawn speech) to spawn a microphone. however deleting the microphone doesn’t work.

Here’s the code:

local cake = game.Workspace.Cake

local speechstand = game.ReplicatedStorage.SpeechThrower

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == "%spawn speech" then
			speechstand:Clone().Parent = workspace
		end
	end)
end)

This is the script that spawns the microphone. Works completely fine.

However, the issue is when I try removing it using the “%remove speech” command

Here’s the code:

local cake = game.Workspace.Cake

local speechstand = game.Workspace:WaitForChild()

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == "%remove speech" then
           speechstand:Destroy()
		end
	end)
end)
2 Likes

Wait, I think I already realized.

1 Like

Because the script will basically yield until the model is parented to workspace, try this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg == "%remove speech" then
           local speechStand = workspace:FindFirstChild("SpeechThrower")
           if speechStand then speechStand:Destroy() end
		end
	end)
end)

Ofc, there are tons of issues with your script but the following should fix your core problem.

1 Like

Yeah, I’m no coder guy I’m just doing the simple stuff. Let me see.

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