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.
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!
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.