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