Help on destroying items

Problem
I want my script to delete a part in the local characters model called part. (It is a part added to a character in Serverscriptservice). The problem is its not destroying the part

Problem 2
Same system as problem one but its with a gui. (It is a template cloned and placed from a module script). The problem is its not destroying the gui too.

I want both the part and gui to be destroyed but its not working. Please any help?

local button = script.Parent

local debounce = false

function onClick()
	local h = button.Parent.Parent.Parent.Parent.Character.Humanoid
	if h then
		local playerName = h.Parent.Name
			local player = game.Players:FindFirstChild(playerName)
			if player then
				local playerGui = player:WaitForChild("PlayerGui")
				if playerGui then
					local noti = playerGui:FindFirstChild("Notifications")
					local container = noti:FindFirstChild("Container")
					local temp = container:FindFirstChild("NotificationTemplate")
	
					if noti then
						if container then
							if temp then
								temp:Destroy()
							end
						end
					end
							
					local ball = h.Parent:FindFirstChild("Clone")
							
					if ball then
						h.Parent:FindFirstChild("Clone"):Destroy()
					end
				else
					warn("Player GUI not found for player: " .. playerName)
				end
			else
				warn("Player not found with name: " .. playerName)
			end
	end
end

script.Parent.MouseButton1Click:Connect(onClick)

Note: The script above is NOT a local script

1 Like

“Note: The script above is NOT a local script” It’s a part of the issue.

You can’t use MouseButton1Click on a Script you need a LocalScript and in your case a RemoteEvent in addition.

Put your RemoteEvent in ReplicatedStorage and use this line of code to find it on your Script and LocalScript.

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") -- Change "RemoteEvent" for the name of your RemoteEvent

When the player click on the button, you detect it with the LocalScript and use FireServer on the RemoteEvent like that to send a signal to the server.

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") 

function onClick()
	RemoteEvent:FireServer()
	-- You can also destroy your Gui here or destroy it on the server it's your choice.
end
script.Parent.MouseButton1Click:Connect(onClick)

In the Script you use OnServerEvent to be able to obtain the signal sent by the clients and process the request. Know that the first argument will always be the player who sent the received signal.

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") 

RemoteEvent.OnServerEvent:Connect(function(player)
	-- Find your part and destroy it on the server.
end)

You can read this article available in the Documentation to better understand what you can do with RemoteEvents.

Let me know if this helped you. You can also ask me your questions if you have any.

3 Likes

Oh I forgot to mention something crucial that’s not mentioned at the top. This script teleports the player to another area so it has to be a script. Right?

1 Like

It depends. If you are only teleporting the client from one place to another you can use the client side function from what I think I understand from the documentation. However, to teleport several players you will have to do it on the server side.

If “teleport to another area” meant moving the character from one position to another in the workspace, then you can do it on the client side because the character will replicate automatically.

2 Likes

The teleporting system is in game and doesn’t transfer to any server. My game is about knocking people off a map and I want it so if you pressed a button (like in this case you get teleported to the lobby) that it destroys the part (aka a ball) and the cooldown aka the temp in the script below.

local noti = playerGui:FindFirstChild("Notifications")
local container = noti:FindFirstChild("Container")
local temp = container:FindFirstChild("NotificationTemplate")

I don’t think you can move a players position with a local script so I am using a script for that.

Yes you can teleport the client’s character with LocalScript because as I said in my last comment, the character is automatically replicated on the server from the client.

Anyway it’s a better pratice to do it on the server side. But you still can’t use MouseButton1Click with a Script. So you need to use a LocalScript to detect the click event, send a request to server from using a RemoteEvent and process at the request on server side like I said in a comment above.

1 Like

I still get the same issue however I didn’t know about the MoveTo option!

Heres the additions to the script that I am using so far

Local Script 1

game.ReplicatedStorage.TeleportLobby:FireServer(player)

Server Script 1

game.ReplicatedStorage.TeleportLobby.OnServerEvent:Connect(function(player)
	local playerGui = player.PlayerGui
	
	local temp = playerGui.Notifications.Container:FindFirstChild("NotificationTemplate")
	local ball = player.Character:FindFirstChild("Clone")
	
	if temp then
		temp:Destroy()
	else
		print("Cant Find Temp")
	end

	if ball then
		player.Character:FindFirstChild("Clone"):Destroy()
	else
		print("Cant find ball")
	end
end)

The prints say both of the

print("Cant Find Temp")
print("Cant find ball")

I don’t know what’s going on because clearly it appears in the Explorer tab. Are module scripts registered on the server side?

The module script is in StarterPlayerScripts so is that issue too?

Sorry for the late reply

You create “temp” and “ball” on the client or the server?

Temp is created with a module script

Ball is created by a local script that fires a event to a script that makes the part in the character

To be clear

  • A LocalScript is a Lua code container that runs its contents on the client (player’s device).
  • A Script is a Lua code container that runs its contents on the server.

When you use a ModuleScript in a Script the code is executed on the server. When you use a ModuleScript in a LocalScript the code is executed on the client.

If temp is created on the client you will have to destroy it on the client.

  • If you create “Ball” on the client-side this one will visible only for this client.
  • If you create an instance on the client-side and try to send it from RemoteEvent, you will get nil on server.
  • If you create “Ball” on the client-side you can’t destroy it on the server-side, destroy it on the client-side.
1 Like

OK, the gui part is fixed, but not the part one

Heres the Script in ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ThrowEvent = ReplicatedStorage.ThrowEvent
local BallsToThrow = ReplicatedStorage:WaitForChild("BallsToThrow")

local gravity = 300
local throwspeed = 200

ThrowEvent.OnServerEvent:Connect(function(Player, mouse)
	wait(0.28)

	local ballName = Player.Index.Ball.Value
	local ballToThrowTemplate = BallsToThrow:FindFirstChild(ballName)

	if ballToThrowTemplate then
		local ThrowItem = ballToThrowTemplate:Clone()
		local offset = Vector3.new(0, 3.5, 0)
		local offsettest = Vector3.new(4, 6.5, 0)
	
		ThrowItem.CFrame = CFrame.new(Player.Character:FindFirstChild("Head").Position + offset, mouse.Position)
		ThrowItem.Velocity = ThrowItem.CFrame.LookVector * throwspeed
		ThrowItem.CanCollide = false
		ThrowItem.Anchored = false
		ThrowItem.Name = "Clone"
		ThrowItem.Parent = Player.Character


		local attachment = Instance.new("Attachment", ThrowItem)
		local vectorforce = Instance.new("VectorForce", ThrowItem)

		vectorforce.Force = Vector3.new(0, ThrowItem:GetMass() * gravity, 0)

		ThrowItem:SetNetworkOwner(Player)
	end
end)

This makes the part script.

Heres the script that deletes it (ServerScriptService)

game.ReplicatedStorage.TeleportLobby.OnServerEvent:Connect(function(player)
	local ball = player.Character:FindFirstChild("Clone")

	if ball then
		ball:Destroy()
	else
		print("Cant find ball")
	end
end)

Ball is on the server side so this script should be working, right?

Two question,

  • Is it in the same script?
  • Can the player have two balls at the same time?
  1. No its seperate
  2. Yes many clones can be produced at one time

Okay so if you have several “clone” how do you know which one need to be deleted and how to recognize it if they all have the same name? :sweat_smile:

Oh yeah I just realised that! Ill figure out a way to do that, give me some minutes.

Yep, that was the problem lol
Thanks for pointing that out and how to do the MoveTo option!
God bless :pray:

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