Script can't find value of stringvalue

In my game, I want to make a button so that when a specific block touches it, the door opens. No other block can open the door.

However, my script cannot find the value of the identifier string value so that the button knows if the block is correct or not.

Here is the code:

local part = script.Parent.Parent

script.Parent.Touched:Connect(function(plr)
	local char = plr.parent
	print(char)
	local RedKey = char:FindFirstChild("BlockKey")
	if RedKey.Value == "1" then
		script.Parent.Script.Disabled = true
		script.Parent.Door.Transparency = 0
		script.Parent.Door.CanCollide = true
		wait(0.1)
		script.Parent.Door.Transparency = 0.35
		script.Parent.Door.CanCollide = false
		script.Parent.Script.Disabled = false
	end
end)

Here is the code output whenever I place the block on the button:
image

Here is what the block looks like with its children:
image

I’ve tried to find a way to scan the value in different ways but to no avail.

1 Like
local RedKey = char.Part:FindFirstChild("BlockKey")

?
I think

plr.Parent would be referring to the Hit’s Parent every time the Touched function is called, I’d check first if RedKey isn’t equal to nil before you check for the Value of it

Also, I’d recommend implementing a debounce first cause that event will fire every time it gets touched by something, and will overlap other Hit parts as well

local Part = script.Parent.Parent
local DB = false

script.Parent.Touched:Connect(function(Hit)
    if DB then return end

    local Char = Hit.Parent

    if Char then
        local RedKey = Char:FindFirstChild("BlockKey")

        if RedKey and RedKey.Value == "1" then
            DB = true
            script.Parent.Script.Disabled = true
            script.Parent.Door.Transparency = 0
            script.Parent.Door.CanCollide = true
            wait(.1)
            script.Parent.Script.Disabled = false
            script.Parent.Door.Transparency = 0.35
            script.Parent.Door.CanCollide = false
            DB = false
        end
    end
end)

I actually ran the script however the game thinks that BlockKey is “nil” but yet the value shows this:
image
So the value is clearly not empty but is still returning as so…
edit: I’ve actually tried to set it within the script but returns with:
image
So the script thinks stringvalues dont have a value descendant

isnt blockkey inside the part and not directily in the character?

but u said local RedKey = char:FindFirstChild("BlockKey")

Your touched event only checks if the touchedpart has a BlockKey but you aren’t detecting if the touchedpart is actually a player. Try this:

local part = script.Parent.Parent

script.Parent.Touched:Connect(function(plr)
	local char = plr.Parent
	print(char)

	if game.Players:FindFirstChild(char.Name) then
		local GrabBlock = char:FindFirstChild("GrabBlock")
	
		if GrabBlock then
			local RedKey = GrabBlock:WaitForChild("BlockKey")
			
			if RedKey.Value == "1" then
				script.Parent.Script.Disabled = true
				script.Parent.Door.Transparency = 0
				script.Parent.Door.CanCollide = true
				wait(0.1)
				script.Parent.Door.Transparency = 0.35
				script.Parent.Door.CanCollide = false
				script.Parent.Script.Disabled = false
			end
		end
	end
end)

so you’re saying char.Part.Value?

That shouldn’t be the case though, unless if your BlockKey object is parented inside your Character Model

Line 28 doesn’t even exist anywhere within my script, can’t you just add print statements to check what you’re exactly finding?

wait so how does

local RedKey = char:FindFirstChild("BlockKey")

work then
if block key is in a part in the char and not in the char?
can someone explain how this works?

Sorry for the very badly named variables but “char” refers to the block, not the player

Ahh I see, so char is the GrabBlock?

Wait sooooooooooooo

What you’re saying is that, Char is actually referring to the Part’s Parent of that Block? That’s the reason why it keeps returning back as nil, because there’s no proper properties to look for if you’re just looking for the part alone inside of Part.Parent, you can just simply do Part

Never mind. I just solved the problem
image
The game went to the parent of the block, so now when char = hit it no longer does that

1 Like

isnt that what i’ve been saying for my past few posts? o-o

It kinda confused us where you even got the Char variable even from :sweat_smile: Just a reminder that Hit refers to the BasePart it touched, and Hit.Parent is the BasePart’s Parent (Not the actual Block inside the StringValue), cause you would be referring to the HitPart (Left Arm, Right Leg, Torso)'s Parent which is the Character Model

@jumpyfunk That’s what I was assuming, but we weren’t really sure on the whole incident :man_shrugging:

My bad, I actually ripped this code from a killblock I made because I didn’t have enough time to make a cleaner script one night :blush: Apologies