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

[Solved] Problem with script not recognizing players backpack?

I've made a detector script which sees if a player has a tool in their backpack called "LavaKey" and then if so opens a door by making it invisible and then waits 8 seconds and closes it. The issue is I cant seem to get the line that sees if the key is in the backpack correct and when I run the game and step on the detector I get an error on line 10 saying that "Backpack is not a valid member of Model" but then after that I get no more errors. (That error may be referring to the door thats on the detector or something but it doesnt have a humanoid. Anyone know what I need to fix? Thx.

local detector = script.Parent
local door = game.Workspace.LavaDoor
DB = true

detector.Touched:Connect(function(hit)
	if DB == true then
		DB = false
		if hit.Parent:FindFirstChild('Humanoid') then
			local hum = hit.Parent:FindFirstChild('Humanoid')
			if hit.Parent.Backpack:FindFirstChild("LavaKey") then--here
				door.Transparency = 1
				door.CanCollide = false
				wait(8)
				door.Transparency = 0
				door.CanCollide = true
			elseif not hit.Parent.Backpack:FindFirstChild("LavaKey") then
				hum:TakeDamage(20)
			end
		wait(2)
		DB = true
		end
	end
	
end)

1 comment

0

i ended up fixing it by using, "local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)"

1 answer

✓ Accepted answer

The backpack is not in the character in workspace, which is where you are looking, you need to access the players backpack in the game.players, which can be done by

game:GetService:("players")

Sorry for not writing out a script that will get the player exactly how you need it to but i think this should answer your question if you're aware on how to get players from the player folder, Let me know!

2 comments

0

you don't need another semi colon after GetService. game:GetService("Players")

0

ok i can do what you wrote but how can i then reference the player that hit the detector as game.GetService("players")

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