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.
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
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!
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.