Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Workspace.Rocket.Script:2: attempt to index nil with 'Name', anybody help?

Asked by 3 years ago
Edited 3 years ago

I am trying to make a COALSCE (hopefully i spelt that right) block, remember that old game where everybody played? Yeah, currently I need to use print("HIT") just to test if it works, and it doesn't. Can somebody please help?

script.Parent.Touched:connect(function(rock)
if rock.Parent.Name == "Rocket" then --this is the name of the rocket
    print("HIT")
end
end)

The error I am getting is:

Workspace.Rocket.Script:2: attempt to index nil with 'Name'

Please help, I really want to make a game like COALSCE (i suck at spelling this) thanks.

1 answer

Log in to vote
0
Answered by 3 years ago

Hi, after talking to you in the community chat I have come up with this. The main problem is you said

if rock.Parent.Name == "Rocket" then

rock is the parameter of the function of a .Touched event. This will output the object that touched the part during the event. So you need to access the name of the object, not the Parent. The parent ends up being workspace (I checked the code of the rocket launcher)

But also, the rocket launcher model you are using isn't the best when it comes to code. The collisions are messed up because of the rocket propulsion. Sometimes the part would detect hits, other times it wouldn't. I recommend using the ROBLOX official rocket launcher, which I found to be the second rocket launcher model that shows up when you search for one. Hover over it and look under the image, it should show "ROBLOX" as the author. That one seems to fire the touch event always.

You should always use a debounce during .Touched events (unless for some reason you can't)

Here's more info on that at https://developer.roblox.com/en-us/articles/Debounce

Let me know if you need anything else.

local db = false
script.Parent.Touched:connect(function(rock)

if not db and rock.Name == "Rocket" then -- if not db means if not true. Rock.Parent.Name would not be correct. The thing that touched it would be the rocket part. Its name is "Rocket" so just use parameter.Name == "Rocket" 
        db = true
      print("HIT")
        db = false -- all of this db stuff is to keep your .Touched event from running too many times. Db stands for debounce. Try to always use it in a .Touched event.
end
end)


Ad

Answer this question