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

Workspace.Door1.Part.Script:2: attempt to index nil with 'UserId' error?

Asked by 3 years ago

I'm trying to make a level door system, I have the database all set up and working but my door is not working with the error in the title. Can anyone give me a hand? Thanks.

local DataStore2 = require(1936396537)
local player = game.Players.LocalPlayer.UserId

script.Parent.Touched:Connect(function()
    local levelStore = DataStore2("Level", player)
    if levelStore <= 1 then
        script.Parent.CanCollide = false
    end
end)

script.Parent.TouchEnded:Connect(function()
    local levelStore = DataStore2("Level", player)
    if levelStore <= 1 then
        script.Parent.CanCollide = true
    end
end)
0
LocalPlayer does not exist on server since it does not know who you mean by local player imKirda 4491 — 3y
0
is it a local script? or a script? SpriteGamerHD 47 — 3y

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

you need to hook up the player from the colliding part ie:

-- mockup DataStore NOT NEEDED!
local DataStore2 = (function(Group, player)  -- require(1936396537)
    local DS = {
        ["Level"] = {
            ["TGazza"] = 0
        }
    }   

    if(DS[Group] ~= nil) then
        if(DS[Group][player] ~= nil) then
            return DS[Group][player]
        end
    end
    return math.huge -- Omg it's Huge!
end)

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(bit)
    local player = Players:GetPlayerFromCharacter(bit.Parent)
    if(player == nil) then return end
    local levelStore = DataStore2("Level", player.Name)
    if levelStore <= 1 then
        script.Parent.CanCollide = false
    end
end)

script.Parent.TouchEnded:Connect(function(bit)
    local player = Players:GetPlayerFromCharacter(bit.Parent)
    if(player == nil) then return end
    local levelStore = DataStore2("Level", player.Name)
    if levelStore <= 1 then
        script.Parent.CanCollide = true
    end
end)

hope this helps! :)

Ad

Answer this question