Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How t o i make a part with a script in it be removed when it touches another part?

Asked by 6 years ago
Edited 6 years ago

here is the script in the part that I want removed if it come in contact with another part

--[[ filler XP--]] function touch(hit) hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health -50

just for notice I want the part removed not the script, and I want the part to came back after 3 secs

0
Could you format your script better? It is hard to read. bluestreakejjp 41 — 6y
0
ok imaginevreything -50 — 6y

2 answers

Log in to vote
4
Answered by
amanda 1059 Moderation Voter
6 years ago

If that is your entire script, let me give you a few tips:

  • You must Connect your function in order for it to be triggered whenever a part is Touched.

  • Before changing the Health of a Humanoid, you should make sure the part touching belongs to a player's character. You can do this by calling GetPlayerFromCharacter on the Players service.

  • In order to make the part disappear for 3 seconds, you should move it to ServerStorage and then back to workspace.

local players = game:GetService("Players")
local storage = game:GetService("ServerStoarage")
local part = script.Parent

local function Touch(hit)
    local character = hit.Parent
    local player = players:GetPlayerFromCharacter(character)
    if player then
        character.Humanoid.Health = character.Humanoid.Health - 50
        part.Parent = storage
        wait(3)
        part.Parent = workspace
    end
end

part.Touched:Connect(touch)
Ad
Log in to vote
0
Answered by 6 years ago

Instead of using :Destroy() or something like that, you can parent the part to nil, then after three seconds it parents back to whatever object it was originally parented to.

(Your formatting of the script was hard to read so this was what i thought it was trying to be) ((I'm also not a very good scripter so don't get mad if this is bad)

whatever = script.Parent.Parent
function touch(hit) 
    hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 50
    script.Parent.Parent = nil
    wait(3)
    script.Parent.Parent = whatever
end

0
Not the best use of FindFirstChild bluestreakejjp 41 — 6y

Answer this question