Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make spawned objects kill?

Hi everyone,

I am spawning large spheres to roll randomly down a slope to form obstacles the player must dodge. However, the objects only collide with the player pushing them back.

To make it harder I would like it to kill or reduce health on contact.

This is my normal hit code to kill the player in other areas such as a lava floor:

script.Parent.Touched:connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
    if h then
       if h.Health > 0 then
	            h.Health = 0
	        end
	    end
	end)

but I need it to fit into the Sphere spawner script below:

local Debris = game:GetService("Debris")
 
local rball = Instance.new("Part")
rball.Anchored = false
rball.Shape = Enum.PartType.Ball
rball.TopSurface = Enum.SurfaceType.Smooth
rball.BottomSurface = Enum.SurfaceType.Smooth
rball.Size = Vector3.new(16, 16, 16)
 
while true do
	newBall = rball:Clone()
	newBall.BrickColor = BrickColor.new("Burlap")
	newBall.CFrame = CFrame.new(250, 160, 140)
	newBall.Material = Enum.Material.Cobblestone
	newBall.Velocity = Vector3.new(math.random(-30, 20), 0, 0)
	newBall.Parent = game.Workspace
	Debris:AddItem(newBall, 12)
	wait(2)
end

Tried all places but no joy, can anyone help?

Thanks again

2 comments

0

Make a ball with these properties and put them into ServerStorage. Then, put the script inside the ball in ServerStorage. Then, reference the ball and clone it

0

Brilliant this has worked, thank you very much :)

2 answers

✓ Accepted answer
local Debris = game:GetService("Debris")

local rball = Instance.new("Part")
rball.Anchored = false
rball.Shape = Enum.PartType.Ball
rball.TopSurface = Enum.SurfaceType.Smooth
rball.BottomSurface = Enum.SurfaceType.Smooth
rball.Size = Vector3.new(16, 16, 16)

while true do
    newBall = rball:Clone()
    newBall.BrickColor = BrickColor.new("Burlap")
    newBall.CFrame = CFrame.new(250, 160, 140)
    newBall.Material = Enum.Material.Cobblestone
    newBall.Velocity = Vector3.new(math.random(-30, 20), 0, 0)
    newBall.Parent = game.Workspace
    newBall.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if h then
       if h.Health > 0 then
                h.Health = 0
            end
        end
    end)

    Debris:AddItem(newBall, 12)
    wait(2)
end

Take your "normal hit code" and put it in a script (for this example name it "kill_script") which is disabled and parented to game.ServerStorage. Now, inside of your spawner script do this...

local Debris = game:GetService("Debris")

local rball = Instance.new("Part")
rball.Anchored = false
rball.Shape = Enum.PartType.Ball
rball.TopSurface = Enum.SurfaceType.Smooth
rball.BottomSurface = Enum.SurfaceType.Smooth
rball.Size = Vector3.new(16, 16, 16)

local kill_script = game.ServerStorage.kill_script:Clone() --clone the script

while true do
    newBall = rball:Clone()
    newBall.BrickColor = BrickColor.new("Burlap")
    newBall.CFrame = CFrame.new(250, 160, 140)
    newBall.Material = Enum.Material.Cobblestone
    newBall.Velocity = Vector3.new(math.random(-30, 20), 0, 0)

	kill_script.Disabled = false --enable the cloned script
	kill_script.Parent = newBall --put the script in the ball

    newBall.Parent = game.Workspace
    Debris:AddItem(newBall, 12)
    wait(2)
end

Hopefully this helps, let me know! :)

1 comment

0

Thanks Alex, looks great and make sense however I'm afraid it hasn't done the trick. I've moved the kill script into Server Storage and updated the spawner script and also tested it online. Thanks for your help though :)

This discussion is preserved as it was. New answers, comments, and votes are closed.