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

Trying to make a kill script work with an instance.new brick?

Asked by 5 years ago
Edited 5 years ago
function onTouched(hit)
    local h = hit.Parent:findFirstChild("Humanoid")
    while true do 
    wait(1.5)
        if (h ~= nil) then
            h.Health = h.Health - 6
            wait(1.5)
        end 
    end
end
print("Works")
game.Workspace.F9Parent:FindFirstChild("F9").Touched:connect(onTouched)

It's suppose to hurt the player when they touch the brick, but it doesn't even print "works" in the output.

The script works with a normal part, however this brick was made with instance.new and there are many of these bricks with the same name. I can't seem to get it to work.

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

first, have a pre-made part with the kill script as its child. That means

script.Parent.Touched:connect(onTouched)

next, move this pre-made part into the serverstorage. The script wont run inthe serverstorage, so dont worry about lag. in serverscriptservice, add a new script with a cloning code:

local deathPart = game.Serverstorage.deathPart
deathPartC = deathPart:clone()
deathPartC.Parent = game.workspace
--position, rotation, and other properties is on you
1
:clone() is deprecated, use :Clone() User#23365 30 — 5y
1
and game.workspace and game.Serverstorage is deprecated use game.Workspace and game.ServerStorage User#23365 30 — 5y
0
i didnt know... EliteRayGawnX2 124 — 5y
Ad
Log in to vote
0
Answered by
Oficcer_F 207 Moderation Voter
5 years ago

You cannot use the same script for multiple bricks with the same name. You best guess is to copy a script like the one above into all of the bricks you spawn with Instance.

Or you can use bindable functions (search it up).

Hope this helps :)

If it does, please accept the answer.

GOOD LUCK!

0
How do I clone a script? Gameplay28 139 — 5y
0
:/ Oficcer_F 207 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Bad boi code

I picked up the following: findFirstChild connect

and also the fact that you need to wait() for the child ;p

try this:

function onTouched(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    while true do 
    wait(1.5)
        if (h ~= nil) then
            h.Health = h.Health - 6
            wait(1.5)
        end 
    end
end
print("Works")
game.Workspace.F9Parent:WaitForChild("F9").Touched:Connect(onTouched)

You do the cloning part........ And then when the part named "F9" is found in workspace, then this code will run..

Oh and! I fixed the connect lower case "c" and findFirstChild lower case "f" also used WaitForChild to pick up the instance once created ;p Goodbye.

Answer this question