How to make a script that deletes a model inside a player using a GUI button?

Before you go hard on me I just want to say that I am a beginner and I need more time to understand what to do.

  1. What do you want to achieve? Keep it simple and clear!

I want to make GUI Button that deletes a model inside the player

  1. What is the issue? Include screenshots / videos if possible!

I keep struggling to end the script

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes I did, however I couldn’t find any.

local remoteEvent = game.ReplicatedStorage:WaitForChild("Acessory")

script.Parent.MouseButton1Click:Connect(function() 
	remoteEvent.OnServerEvent:Connect(function(player)
	for i, v in pairs(player.Character:GetDescendants()) do
		if v.Name == "CircleGlasses" then
				v:Destroy()
1 Like

It seems that this code is in the same script. So, first make sure that you have a Script in ServerScriptService, and make sure that you have your LocalScript in StarterGui, where the GuiButton is, or somewhere where LocalScripts can run like StarterPlayerScripts or StarterCharacterScripts.

Inside of the LocalScript, all you need to do is fire the RemoteEvent when the GuiButton gets pressed.

Client example:

button.Activated:Connect(function()
    remote:FireServer()
end)

And instead of setting up a RemoteEvent.OnServerEvent event listener in the LocalScript, set it up in the Script (server-sided script). Only the server can detect the OnServerEvent event.

Server example:

remote.OnServerEvent:Connect(function(player)
    -- Use `Instance:FindFirstChild()` to get children and descendants.
    -- Pass `true` as the second argument to search through the descendants
    -- if there aren't children with the provided name.
    local model = player.Character:FindFirstChild("CircleGlasses", true)

    -- Check if the model exists (if it isn't `nil`).
    -- If the model does exist, destroy it.
    if model then model:Destroy() end
end)
2 Likes

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