Need help with my script

So I’m creating a script, so that if a player touches grass, an event will happen.
I want to make it so that if any of the children (the grass) in a folder is touched by a player, the event happens, so that I don’t have to add the script for every single blade of grass

But when I run the code, it says this

Workspace.Folder.Script:9: attempt to call missing method 'IsA' of table

Can anyone tell me how to fix this?

local grass = script.Parent:GetDescendants("Grass")
local debounce = false
local cooldown = 0.2

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

for _, part in ipairs(grass) do
	if grass:IsA("BasePart") then --ERROR IS HERE
		grass.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then --hit.Parent should be character
				if not debounce then
					debounce = true
					print("Player has touched grass")
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					remoteEvent:FireClient(player)
					wait(cooldown)
					debounce = false
				end
			end
		end)
	end
end

Thanks

for _, part in script.Parent:GetDescendants() do
	if part:IsA("BasePart") then --ERROR IS HERE
		part.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then --hit.Parent should be character
				if not debounce then
					debounce = true
					print("Player has touched grass")
					local player = game.Players:GetPlayerFromCharacter(hit.Parent)
					remoteEvent:FireClient(player)
					wait(cooldown)
					debounce = false
				end
			end
		end)
	end
end

my guy. you are checking if a TABLE is a part, not if “part” is a Part

1 Like

where did the local grass = part come from?

also i have no idea how to use ipairs so ye

Did my snippet work?

I suggest using pairs or nothing in a for i, v loop:

for i, v in pairs(grass) do
    print(v) --> Part, Part, etc
end

for i, v in grass do
    print(v) --> Part, Part, etc
end

The issue is a simple typo, you meant to do part:IsA("BasePart"), not grass:IsA("BasePart").

(Don’t mark this as solution)

no, it says part is an error type or smth

Can I see the error screenshot?

Replace if grass:IsA("BasePart") then with if part:IsA("BasePart") then Because you are not checking the part that gets returned. Instead you are cheking if a table is a part which will always return nil or print a error

this is the same solution as dev4q, so i got the connect with nil error too

i got a different error with nil and connect
@pumpkyrofl’s solution worked tho so its alright now

thanks for helping anyway

edit: not weakroblox, its pumpky mb

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.