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

Ontouch script not working?

So basically all I want this script to do if a zombie touches it the health on the gui goes down by one. But for some reason it's not working and I think I did something wrong.

function onTouch(part) 
local humanoid = part.Parent:FindFirstChild("Humanoid")
local zombie =  part.Parent:FindFirstChild("Humanoid").Name == "Drooling Zombie"


local n = PlayerGui.Notice.Box.Health1.Health.Value


if humanoid~=nil and zombie~=nil then
	for i, v in ipairs(game.Players:GetChildren()) do
		v.n = (n.Value -1)
		v.PlayerGui.Notice.Box.Health1.Text = n.Value
	end
	
end
end

script.Parent.Touched:connect(onTouch)

4 comments

0

is the humanoid's name Drooling Zombie?

0

Yes

0

FirstFindChild returns a child with the same name as the string. so if you try to find the humanoid's name, it should be humanoid.

0

@Hungryjaffer I just fixed it so finds the models name as Drooling zombie and it still does not work

2 answers

On your function parentheses you put in part instead of hit so maybe Lua is not picking it up as a touch/hit

function onTouch(hit) local humanoid = part.Parent:FindFirstChild("Humanoid") local zombie = part.Parent:FindFirstChild("Humanoid").Name == "Drooling Zombie"

local n = PlayerGui.Notice.Box.Health1.Health.Value

if humanoid~=nil and zombie~=nil then for i, v in ipairs(game.Players:GetChildren()) do v.n = (n.Value -1) v.PlayerGui.Notice.Box.Health1.Text = n.Value end

end end

script.Parent.Touched:connect(onTouch)

if not im not sure what it is.

1 comment

0

No. Between parentheses you can put whatever you want on Touched event.

You did something wrong there:

local zombie =  part.Parent:FindFirstChild("Humanoid").Name == "Drooling Zombie"

local zombie = is supposed to be used as a variable creation. I don't know if your trying to create a variable or modify the humanoids name.

If your trying to modify the humanoids name, don't use local zombie=. Use:

humanoid.Name == "Drooling Zombie"

Also, if you want to modify a value of a Value Object you need to make sure that you have valueObject.Value:

valueObject.Value = whateverValue

On line 12, your not putting v at the start of n.Value. The script doesn't know whats n because there's no n variable. So i think it would be v.PlayerGui.Notice.Box.Health1.Text = v.n.Value

So the script would be:

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	humanoid.Name == "Drooling Zombie"


	local n = PlayerGui.Notice.Box.Health1.Health.Value


	if humanoid~=nil then
		for i, v in ipairs(game.Players:GetChildren()) do
       			v.n = (n.Value -1)
      			v.PlayerGui.Notice.Box.Health1.Text = v.n.Value
		
   	 end
end

script.Parent.Touched:connect(onTouch)

Protip: Use The output.

1 comment

0

I still have one problem it only loses health 1 time what I do?

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