Why isnt this script working?

Hello, I am making a space game. There are enemies. When the enemies are hit, they don’t do what they are supposed to do; flash white and take damage.

This is my script:

local Chicken = script.Parent

function NewChicken()
	local ChickenClone = Chicken:Clone()
	ChickenClone.Parent = workspace

	local SpinScriptClone = ReplicatedStorage.Assets.Scripts.Spin:Clone()
	SpinScriptClone.Parent = ChickenClone
	SpinScriptClone.Enabled = true

	local DespawnScriptClone = ReplicatedStorage.Assets.Scripts.ChickenDespawn:Clone()
	DespawnScriptClone.Parent = ChickenClone
	DespawnScriptClone.Enabled = true
	
	ChickenClone.Size = (Chicken.Size/2)
end

Chicken.Touched:Connect(function(hit)
	if hit.Parent.Name == "LaserProjectile" then
		local Highlight = ReplicatedStorage.Assets.HitHighlight:Clone()
		Highlight.Parent = Chicken
		
		Chicken.Health -= 1
		
		wait(.1)
		
		Highlight:Destroy()
		if Chicken.Health == 0 then
			NewChicken()
			Chicken:Destroy()
		end
	end
end)

And this makes it:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Chicken = ReplicatedStorage.Assets.Chicken

local Playing = ReplicatedStorage.Values.GameIsPlaying

function SpawnChicken()
	local ChickenClone = ReplicatedStorage.Assets.Chicken:Clone()
	ChickenClone.Parent = workspace
	
	local SpinScriptClone = ReplicatedStorage.Assets.Scripts.Spin:Clone()
	SpinScriptClone.Parent = ChickenClone
	SpinScriptClone.Enabled = true
	
	local DespawnScriptClone = ReplicatedStorage.Assets.Scripts.ChickenDespawn:Clone()
	DespawnScriptClone.Parent = ChickenClone
	DespawnScriptClone.Enabled = true
	
	local ChickenScript = ReplicatedStorage.Assets.Scripts.LargeChickenScript:Clone()
	ChickenScript.Parent = ChickenClone
	ChickenScript.Enabled = true
end

ReplicatedStorage.Events.Chickens.OnServerEvent:Connect(function(player)
	if Playing.Value == true then
		while Playing.Value == true do
			print("Loop Began")
			task.wait(math.random(2.5, 4))
			SpawnChicken()
			print("Chicken Spawned")
		end
	end
end)
1 Like

Have you tried putting a print statement in your Chicken.Touched event? Maybe print out hit, hit.Name, hit.Parent and hit.Parent.Name just to see if your event is getting triggered and by what. Do this before your if statement.

You need to check what part was hit to make sure it matches the part name you’re looking for.
Is the LaserProjectile a model or have any parts in it? You need to check both hit and hit.Parent’s names, if the LaserProjectile is a single part then the script will check for it’s parent (which isn’t a laser)

Okay, this could help:

local Chicken = script.Parent

function NewChicken()
    local ChickenClone = Chicken:Clone()
    ChickenClone.Parent = workspace

    local SpinScriptClone = ReplicatedStorage.Assets.Scripts.Spin:Clone()
    SpinScriptClone.Parent = ChickenClone
    SpinScriptClone.Enabled = true

    local DespawnScriptClone = ReplicatedStorage.Assets.Scripts.ChickenDespawn:Clone()
    DespawnScriptClone.Parent = ChickenClone
    DespawnScriptClone.Enabled = true

    ChickenClone.Size = (Chicken.Size / 2)
end

function DamageChicken()
    local Highlight = ReplicatedStorage.Assets.HitHighlight:Clone()
    Highlight.Parent = Chicken

    Chicken.Health = Chicken.Health - 1

    wait(0.1)

    Highlight:Destroy()

    if Chicken.Health <= 0 then
        NewChicken()
        Chicken:Destroy()
    end
end

Chicken.Touched:Connect(function(hit)
    if hit.Parent.Name == "LaserProjectile" then
        DamageChicken()
    end
end)

Check for the ^ also, they could fix your problem.

it gave no output. filllerrrrrrrr

Screenshot 2023-11-03 at 3.18.50 PM