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

How to call a function when an element from array is touched?

Asked by 3 years ago
Edited 3 years ago

So I'm trying to do a script that prints something when the circle it's touched by a player. (workspace > folder > model > "Circle") But I still get the error attempt to call a nil value

visible_planets = workspace.planets:GetDescendants()
hidden_planets_folder = game.ReplicatedStorage.planets

circles = {}

for key, model in pairs(visible_planets) do

    if model.ClassName == "Model" then
        circle = model:FindFirstChild("Circle")
        table.insert(circles, circle)
        print(table.getn(circles))
    end
end

function get_planet(x)
    local partParent = x.Parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        print('ball touched')
    end
end

for k,v in pairs(circles) do
    v.Touched:Connect(get_planet(v))
end

0
Error basically means you tried to call a value from a function that is nil. JesseSong 3916 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Remember that when a function as an argument with a () is provided, that argument will be equal to what that function returns, not the function itself. The solution here is to remove the (v). The 'hit' argument will still be provided.

0
Thank you! It worked. TheChi_1705 22 — 3y
0
Not to ask for reputation, but please accept my answer. deeskaalstickman649 475 — 3y
Ad

Answer this question