Trying To Delete A part if a string value is something

You can write your topic however you want, but you need to answer these questions:

In my game I am making a building simulator type game.
I have a button to delete all of your work only, but leave the others.
I am trying to use this script to do that but it didn’t work:

The issue is it not deleting my parts that I have placed.

script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Parent.Parent.Parent.Name
	local RS = game:GetService("ReplicatedStorage")
	
	if workspace:IsA("StringValue") then
		if workspace:IsA("StringValue").Value == name then
			workspace:IsA("StringValue").Parent:Destroy()
		end
	end
end)
1 Like
script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Parent.Parent.Parent.Name
	local RS = game:GetService("ReplicatedStorage")
	
	if workspace:IsA("StringValue") then
		if workspace:IsA("StringValue").Value <= name then
			workspace:IsA("StringValue").Parent:Destroy()
		end
	end
end)

Doesn’t, work. Thanks for trying tho! :pensive:

script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Parent.Parent.Parent.Name
	local RS = game:GetService("ReplicatedStorage")
	
	if workspace:IsA("StringValue") then
		if game.workspace:IsA("StringValue").Value == name then
			game.workspace:IsA("StringValue").Parent:Destroy()
		end
	end
end)

I don’t think having greater than or equal to a string is possible, only for numbers

From what I can tell, it seems like you’re trying to get the name of the ScreenGui. Are you? Also, please don’t use server scripts for UI. Use a client script instead

No, I am not trying to get the name of the gui, I am trying to get the name of the player. Also, ok!

Nevermind, I read that wrong. If you are getting the name of the player, using .LocalPlayer would be easier and more efficient. Change this to a client script

Ok, I have changed it to a local script. It still doesn’t work.

I think that there may be a problem with finding the string value.

You’re going to have to edit it to make sure it fits. If needed, use an event to pass information between server/client. Can I see your script?

Sure. The script for deleting or placing?

Here is the one for deleting:

script.Parent.MouseButton1Click:Connect(function()
	local name = script.Parent.Parent.Parent.Parent.Parent.Name
	local RS = game:GetService("ReplicatedStorage")

	if workspace:IsA("StringValue") then
		if workspace:IsA("StringValue").Value == name then
			workspace:IsA("StringValue").Parent:Destroy()
		end
	end
end)

Assuming this is how you’re getting the player’s name? If so, considering you switched to a local script, a more reliable way would be

local Player = game:GetService(“Players”).LocalPlayer
local name = Player.Name

Ok, but how to fix the deleting part??

What is this line for? Why are you checking if Workspace is a StringValue?
It’s not, so nothing is getting passed this line.

1 Like

No, I am trying to circle through every single thing in workspace to see if there is a string value, if so check the value.

Well, the first issue I see it this. From my understanding, this would check if the workspace’s class is a StringValue.

Perhaps use

workspace:FindFirstChildWhichIsA(“StringValue”)

Edit: Note you’d likely have to use a loop in order to get EVERYTHING that matches, and then exit the loop when nothing matches anymore.

1 Like

:IsA() checks if a certain instance is a type object.
e.g a Part is a BasePart, in :IsA() terms.

You can replace it with a for loop.

for a,b in pairs(workspace:GetDescendants()) do
   if b:IsA('StringValue') then

   end
end

You are checking if workspace is a StringValue, and not if any descendants are StringValue’s. Use for i, v in pairs.

Try this in a local script:

local player = game:GetService("Players").LocalPlayer
local name = player.Name

script.Parent.MouseButton1Click:Connect(function()
    for i, v in pairs(workspace:GetDescendants()) do
	    if v:IsA("StringValue") then
		    if v.Value == name then
			    v.Parent:Destroy()
            end
		end
	end
end)

(formatting is broken idk why)

Is there any errors in the output?

Edit: nvm you probably used the code I put before I edited my mistakes)