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
Am I doing something wrong, or is something wrong with roblox?
(edit: fixed code being pasted wrong)