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

Attempt to call a nil value?

Asked by 3 years ago
Edited 3 years ago

Every time I try to touch anything inside game.Workspace.Cubes it would give me an error saying Attempt to call a nil value, I don't what line is making the error but it would bring me to line 4.

local Provider = require(game.ServerScriptService.DataProvider)

local function onTouched(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent)

    if player then
        local data = Provider:Get(player)
        if data then
            table.insert(data.collected,#data.collection + 1,part.Name)
            print("Player"..player.UserId.."Found"..part.Name)
        end
    end
end

for _, v in pairs(game.Workspace.Cubes:GetChildren()) do
    if  v:IsA("BasePart") then
        v.Touched:Connect(onTouched(v))
    end
end
1
Mate, you don't need the extra '(v)' in line 17. AProgrammR 398 — 3y

1 answer

Log in to vote
0
Answered by
A_Mp5 222 Moderation Voter
3 years ago

I find get player from character buggy. Get player from character's parameter is supposed to be the character model a player has.

local Provider = require(game.ServerScriptService.DataProvider)

local function onTouched(part)
    local player = game.Players[part.Parent.Name]

    if player then
        local data = Provider:Get(player)
        if data then
            table.insert(data.collected,#data.collection + 1,part.Name)
            print("Player"..player.UserId.."Found"..part.Name)
        end
    end
end

for _, v in pairs(game.Workspace.Cubes:GetChildren()) do
    if  v:IsA("BasePart") then
        v.Touched:Connect(onTouched())
    end
end

I beleive that part was glitching because the extra v in "v.Touched:Connect(onTouched(v))" which set a nil parameter, and partr was called on line 4 but was nil.

Ad

Answer this question