How do I make it so that .Touched events don't work on a certain part

I want to make it so that a .Touched event won’t work on a certain part (or, if you want why, I want to make it so that a part can’t be deleted from a .Touched event).

The issue I’m having is that I can’t find any sources or way to do so, and if I did find something that people say ‘works’ I didn’t understand it well enough to use it.

Solutions I’ve tried include something called “wrapping” but it only works in that certain script, I need to make it so all scripts can’t delete the part. Is there any way to do this for all scripts?

3 Likes

What are you trying to accomplish exactly cause I’m not really understanding it that well, could you give an example of what you’re trying to do

So, if someone else is using a script such as:

script.Parent.Touched:Connect(function(hit) 
    hit:Destroy() 
end)

I don’t want it to work on a certain part/s.

There’s a few ways you could that, if you want it to not work for specific parts, depends on what type of parts they are. If they are non Character part/not a limb, you can put all the excluded parts in a Folder in workspace for example, and you an just do

script.Parent.Touched:Connect(function(hit) 
    if hit.Parent == workspace.Exclusions then return end --Exclusions is the name of our folder, if it has it as the parent of the part that touched another part, don't destroy
    hit:Destroy() 
end)

Basically, just put the excluded parts somewhere, anywhere and run some code that’ll prevent those parts from being destroyed

This won’t work in my case, since the scripts that are put in are from other people (a script builder basically [not my game]).

If the scripts are put from other people, then what’s stopping them from just making some code to destroy the part? The only way I know you could somewhat prevent that is to just check if something got removed from workspace and if it’s a part you didn’t want gone, just Clone it from replicatedstorage and put it back.

Basically, place the parts wherever and copy them to ReplicatedStorage with all the properties you want already set then you can just run some code

local cantremove =  game.ReplicatedStorage.UnremoveableParts:GetChildren() --We get all the parts you can't remove from a folder

workspace.ChildRemoved:Connect(function(child)
    for i,v in pairs(cantremove) do
        if child.Name == v.Name  then --We'll use their name in this case
            local clone = v:Clone()
            clone.Parent = workspace
            break
        end
    end
end)

This is one way how you could do that, but it would be slow and probably unoptimal given the right conditions. If a part you didn’t want gone was removed, then it will place it back in workspace, you don’t really need to set the Position or anchors since you already did that when the part was copied to a folder in ReplicatedStorage. This will require the unremoveables to have a unique name if possible to not cause conflict

I thought of doing this, but in my case it’s actually the character I don’t want destroyed (it would result in death if the torso or head got destroyed).

In terms of preventing the Character from not being destroyed, that’s a bit more difficult to do since it’s not as simple as the previous example was, nor do I know of a proper way you could instantly respawn the player’s character and put it back where it was. This is a problem with Script Builders, it’s kinda anarchish, there’s no proper way to prevent players from doing a specific thing.

What you could try is in every Character there’s a script that checks if they died and they don’t have a Head or a Torso, which shows that they died via :Destroy(), and if they were killed by that, store the CFrame of the HumanoidRootPart if they didn’t destroy it too, load the character again and move them to their original position

I know how to do this simply, but if I did I would have to run my script again I think.

If players are creating code on their client, they won’t be able to affect other players characters.

Sure if I make a script that deleted all other characters but my own, they will be deleted on My client, but not on their.
The server won’t replicate that unless you provide a means to do so (sending coding to the server to run).

If that’s the case, you should look into parsing the clients code for inappropriate commands or actions.

With a script builder, they can insert regular scripts.

I don’t think you have to run the script again, which one are you referring to?

Well, I mean with my current require script that I have made, it requires the script to be reran when the character respawns.

If that’s the case you likely have Filtering Enabled disabled, in which case you’re just asking for trouble.

I’d strongly recommend setting up a system to incorporate FE, or only run code on the client (doesn’t replicate).

If you just allow the player to run their own scripts client side, there won’t be any issues of players creating malicious code to affect others.

If the player decides to delete their own character, or any other required instance, you could make a client script to detect that and reset the player so they don’t need to leave.

I believe you can do that using the CharacterAdded event to check that once the character respawns, jsut rerun the code, unless it’s a problem to do so

Script builders are meant for allowing this, they allow anyone to execute scripts in the game, that’s what the game is about.

I could, but that would mean in order to run a different script I’d have to rejoin and I don’t want that, it might be possible for me to connect the function to an if statement that can check if I actually want it disabled though…

You could try that and see how it goes, I have no experience in Script Builders so I’m not what the best approaches could be

Yea, I’ll try it out later, got school right now though so I’ll reply on how it went later.

If you want, you can also DM me on discord: Crazycarkids#0181