Attribute becoms nil for no reason?

Im making a crouching system where

  • player will stay crouched no matter what if under an object
  • player will automatically un-crouch if they are no longer under something (and arent holding down crouch)

This code is run on the server, and is fired through a remote event whenever the player presses or un-presses the crouch button
This system here is meant to begin checking whenvever the player unpresses crouch, spawning a repeat loop (only once thanks to tags) that will check if the player is no longer above something and isnt holding down crouch anymore. If the player will leave an obstruction but continue to hold crouch, they will stay crouched

Also the “CheckIfCanUncrouch” function is just a simple raycast that returns true if nothing is hit

For some reason, the attribute has its correct value, but becomes nil shortly after

local function OnRequest(plr : Player, reqName : string, addedInfo : any)
	
	local char = plr.Character
	local result
	
	if reqName == "Crouch" then
		
		plr:SetAttribute("Crouching", true)
		char.Head.CanCollide = false
		char.Head.CanQuery = false
		
	elseif reqName == "Uncrouch" then
		
		plr:SetAttribute("Crouching", false)
		print(plr:GetAttribute("Crouching")) -- first print happens here
		if plr:HasTag("crouchCheck") == false then
			plr:AddTag("crouchCheck")
			task.spawn(function()
-- next prints happen here, for a single time its normal but then becomes nil
				repeat task.wait(0.05) result = CheckIfCanUncrouch(plr) print(result, plr:GetAttribute("Crouching")) until result == true and plr:SetAttribute("Crouching") == false
				char.Head.CanCollide = true
				char.Head.CanQuery = true
				plr:RemoveTag("crouchCheck")

			end)
			
		end
		
	end	
	
end

image

Am I doing something wrong, or is something wrong with roblox?
(edit: fixed code being pasted wrong)

1 Like

You set the attribute to nil in your repeat-until loop when you write:

plr:SetAttribute("Crouching") == false

I believe you mean to use Instance:GetAttribute. You likely made this mistake and struggle to find it due to the fact the line of code it calls home is a long-winded one-liner. This is a HUGE CRIME against code readability; you should rarely be writing one-liners

Official Roblox style guide on whitespace:

image

Hi, you did a typo. You are using set instead of get.

Fixed code:

local function OnRequest(plr : Player, reqName : string, addedInfo : any)

	local char = plr.Character
	local result

	if reqName == "Crouch" then

		plr:SetAttribute("Crouching", true)
		char.Head.CanCollide = false
		char.Head.CanQuery = false

	elseif reqName == "Uncrouch" then

		plr:SetAttribute("Crouching", false)
		print(plr:GetAttribute("Crouching")) -- first print happens here
		if plr:HasTag("crouchCheck") == false then
			plr:AddTag("crouchCheck")
			task.spawn(function()
				-- next prints happen here, for a single time its normal but then becomes nil
				repeat task.wait(0.05) result = CheckIfCanUncrouch(plr) print(result, plr:GetAttribute("Crouching")) until result == true and plr:GetAttribute("Crouching") == false
				char.Head.CanCollide = true
				char.Head.CanQuery = true
				plr:RemoveTag("crouchCheck")

			end)

		end

	end	

end```
1 Like

Thanks for solving my issue, and for the tip on writing code!

1 Like

Thanks for your help, appreciate it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.