Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

LocalPlayer does not exist on server since it does not know who you mean by local player

0

is it a local script? or a script?

1 answer

✓ Accepted answer

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! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.