Hey!
I’ve been trying to make a sprint system where it automatically end the sprint when the player’s primaryPart gets anchored.
For this, I’ve tried using a :GetPropertyChangedSignal("Anchored"):Connect(function() inside a .CharacterAdded function in starterPlayerScripts to detect the property change.
Oddly this only seems to catch anything like 10% of the time. This goes as well for the normal .Changed event.
My code:
local function charAdded(character : Model)
if not character then print("Character not found somehow") return end
character.PrimaryPart:GetPropertyChangedSignal("Anchored"):Connect(function()
-- This never detects anything for some reason
if character.PrimaryPart.Anchored == true then
module.EndSprint(true)
end
end)
warn("doneeeeee")
end
charAdded(workspace:WaitForChild(player.Name))
player.CharacterAdded:Connect(charAdded)
Does anyone know how I could make it 100% reliable? Any help would be appreciated!
I think using a primary part is unnecessary. I would use the HumanoidRootPart rather than a PrimaryPart. It can be used with GetPropertyChangedSignal too.
In your script you’re relying if the PrimaryPart exists and it can possibly error out if it’s called early. I would rather use WaitForChild for the HRP. You could do a fallback when calling the PrimaryPart (by doing both with a or statement) but its entirely up to you.
--Script in StarterPlayer.StarterCharacterScripts
local character: Model = script.Parent
local root: Part = character:WaitForChild("HumanoidRootPart")
root:GetPropertyChangedSignal("Anchored"):Connect(function()
if not root.Anchored then return end
module.EndSprint(true)
end)
If you want to listen to client side changes of the .Anchored property (when a client changes the property value) then you need to put the code in a LocalScript instead.
Not exactly. The characteradded event fires when the character is set to a non-nil value. It also ensures that the most important bodyparts like humanoid and the primary is fully loaded in before sending off the signal.