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

Event Not Working, Touched is not A Valid Member of Model?

So I made a health pack, and I used a simple function:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local hum = hit.Parent.Humanoid
		if hum.Health < hum.MaxHealth and hum.Health > 0 then
			hum.Health = hum.Health + 30
		end
	end
end)

But when I play the game it says, touched is not a valid of Model, and I mean obviously! Any idea what I did wrong, cause I'm obviously not trying to reference a part inside the pack.

Hope you can help, and thank you for your time.

1 comment

0

Touched is nil in a model. This means that it doesn't even exist in models. Well, the event 'touched' or 'clicked' can only be used in parts. Try using the Base parts, so if you are using a model instead of a part, that won't work. Other than that, it might be a weird Studio glitch as it thinks that Touched is an object, not an event, and it sees that there are no objects called "Touched."

4 answers

✓ Accepted answer

Your problem is that the touched event only works with parts. I have two suggestions to help you fix this problem. First: You might want to have the biggest part in the model have the touched event in it. Second(this is the one I recommend): Have a transparent part that is not CanCollidable cover the whole medpack and when it is touched fire the event. Hope this helps. Have a great day scripting!

2 comments

0

To extend on this, make the brick the PrimaryPart of the model, then just change hit.Parent to hit.Parent.PrimaryPart

0

Thanks you so much!

The .Touched event only works on parts, not models.

Hum = hit.Parent:FindFirstChild ("Humanoid") If Hum ~= nil then --script.Parent must be a part--

Hello Flippin, I know that you´ve solved this problem a while ago, but i want to put the solution i came up for my problem, with the help of: User#21908 WizyTheNinja Aaaand of course you.

in my case I want a platform to fade by the PrimaryPart when Touched.

  local part=script.Parent--the model
local fadingpart=part.Part--part i will have to fade
local active=false--debounce purposes
local function fading(OtherPart)
	local partparent=OtherPart.Parent--searching for a child in the part that touches the primary part
	local human=partparent:FindFirstChild("Humanoid")--this child have to be an Humanoid
	if not active and human then
		active=true--debounce purposes
		for n=1,10,1 do
			fadingpart.Transparency=n/10
			wait(0.2)
		end
		fadingpart.CanCollide=false
		wait(3)
		fadingpart.Transparency=0
		fadingpart.CanCollide=true
		active=false--debounce purposes
	end
end
part.PrimaryPart.Touched:Connect(fading)--triggered when the primary part is touched

If I´m misunderstanding something please tell me. Also, correct my writing if necessary. have a great night everyone.

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