Something wrong with my script

I do not understand why my script does not work at all and it is one line that gives an error and if this line is removed, there will be no errors.

Script(This script is copied into a large sphere that will be visible in the video.):

db = false

script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		local model = hit:FindFirstAncestorOfClass("Model") or hit.Parent:FindFirstAncestorOfClass("Model")
		if model then 
		if model.Name ~= "Body" then
		if model:FindFirstChild("Humanoid") then
			local blood = game.ReplicatedStorage.Effects.Blood.Blood:Clone()
			blood.Parent = model.PrimaryPart
			blood:Emit(30)
			model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed - 6
			model.Humanoid:TakeDamage(6)
			script.Parent:Destroy()
			blood:Destroy()
			end
		end
		end
		wait(1)
		model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed + 6 -- error line
	end
	db = false
end)

Can you send a picture of how your character is layed out?

2 Likes

Youre attempting to set the Humanoids WalkSpeed outside of the Humanoid Check.

I would recommend having this line of code, right after it gets the model:

if not model:FindFirstChildOfClass("Humanoid") then return end

This should just stop the rest of the code from running.

I would also recommend using task.wait() instead of wait() as wait() is deprecated.

2 Likes

Yes.
image

Made like this, and i’m still have this error

db = false


script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		local model = hit:FindFirstAncestorOfClass("Model") or hit.Parent:FindFirstAncestorOfClass("Model")
		if not model:FindFirstChildOfClass("Humanoid") then return end
		if model then 
		if model.Name ~= "Body" then
		if model:FindFirstChild("Humanoid") then
			local blood = game.ReplicatedStorage.Effects.Blood.Blood:Clone()
			blood.Parent = model.PrimaryPart
			blood:Emit(30)
			model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed - 6
			model.Humanoid:TakeDamage(6)
			script.Parent:Destroy()
			blood:Destroy()
			end
		end
		end
		task.wait(1)
		if not model:FindFirstChildOfClass("Humanoid") then return end
		model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed + 6 -- error
	end
	db = false
end)

Attempt moving the Humanoid speed addition part inside of the blood effect part of the script.

db = false


script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		local model = hit:FindFirstAncestorOfClass("Model") or hit.Parent:FindFirstAncestorOfClass("Model")
		if not model:FindFirstChildOfClass("Humanoid") then return end
		if model then 
		if model.Name ~= "Body" then
		if model:FindFirstChild("Humanoid") then
			local blood = game.ReplicatedStorage.Effects.Blood.Blood:Clone()
			blood.Parent = model.PrimaryPart
			blood:Emit(30)
			model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed - 6
			model.Humanoid:TakeDamage(6)
			script.Parent:Destroy()
			blood:Destroy()
			task.wait(1)
			model.Humanoid.WalkSpeed = model.Humanoid.WalkSpeed + 6 -- i just moved it here.
			end
		end
		end
	end
	db = false
end)

This works. Error gone, but script not work. I can’t damage humanoids.

Try this:
This code first creates a referance to humanoid by doing:

local Humanoid = model:FindFirstChildOfClass("Humanoid")

And then it tries to change walk speed
(I haven’t tested it but I think it will work)

Full Code:

db = false


script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		local model = hit:FindFirstAncestorOfClass("Model") or hit.Parent:FindFirstAncestorOfClass("Model")
		if not model:FindFirstChildOfClass("Humanoid") then return end
		if model then 
			if model.Name ~= "Body" then
				if model:FindFirstChildOfClass("Humanoid") then
					local Humanoid = model:FindFirstChildOfClass("Humanoid")
					local blood = game.ReplicatedStorage.Effects.Blood.Blood:Clone()
					blood.Parent = model.PrimaryPart
					blood:Emit(30)
					Humanoid.WalkSpeed = Humanoid.WalkSpeed - 6
					Humanoid:TakeDamage(6)
					script.Parent:Destroy()
					blood:Destroy()
					task.wait(1)
					Humanoid.WalkSpeed = Humanoid.WalkSpeed + 6
				end
			end
		end
	end
	db = false
end)

This helped alot. But i have a question. Why can’t I do damage to Dummy. For example, I have attack script and when I attack Dummy takes damage. And now I can’t damage him with this tool. But when I took an NPC from the toolbox that can walk, it takes damage.

I tried moving them during the attack and they still didn’t take damage. But when I set Anchor to false, the block fell and caused damage to them. How does it work?

OFFFF. I’m know why. I moved dummy from the client. XD. Thanks