How Can I Check Attribute with InputBegan?

Everything in this script works, however, I want to make it so if an attribute is set to true, it would stop the run() and stop() function. How can I achieve this? I’ve tried repeat, getpropertychangedsignual, but they just break the iskeydown function.

I’ve marked the problem below. (scroll to the right to see it)

repeat task.wait() until game:IsLoaded()

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")


local Running = Character:SetAttribute("Running", true)
local notMoving = true
local isOnGround = true
local Jumped = false
local rundb = false
local lastPosition = Character.PrimaryPart.Position

local alreadyPressed = false

local RunButton = Enum.KeyCode.W
local newSpeed = Character:GetAttribute("RunSpeed")

Character:GetAttributeChangedSignal("RunSpeed"):Connect(function()
	newSpeed = Character:GetAttribute("RunSpeed")
end)

local function Run()
	if isOnGround == true then
		Humanoid.WalkSpeed = newSpeed
	end
end

local function Stop()
	Humanoid.WalkSpeed = Character:GetAttribute("WalkSpeed")
end

UIS.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	if UIS:IsKeyDown(RunButton) and Character:GetAttribute("Stunned") == false and Character:GetAttribute("Blocking") == false and alreadyPressed == false then --the problem
		Run()
		alreadyPressed = true
	end
end)

UIS.InputEnded:Connect(function(input, isTyping)
	if isTyping then return end
	if not UIS:IsKeyDown(RunButton) and Character:GetAttribute("Stunned") == false and Character:GetAttribute("Blocking") == false and alreadyPressed == true then --the problem
		Stop()
		alreadyPressed = false
	end
end)
1 Like

The Run and Stop functions are already done executing when they are called by the InputBegan and InputEnded callbacks, but the WalkSpeed does not reset. Functions are like a list of instructions in a cooking recipe, and any instructions done cannot be undone. Likewise, there is no stopping a set of instructions if it is done (with some exceptions like while loops).

Since the Stop function resets the property Humanoid.WalkSpeed, use GetAttributeChangedSignal to call that function so the character stops running.

Character:GetAttributeChangedSignal("Stunned"):Connect(function()
	if Character:GetAttribute("Stunned") == true then
		Stop()
	end
end)

Character:GetAttributeChangedSignal("Stunned"):Connect(function()
	if Character:GetAttribute("Blocking") == true then
		Stop()
	end
end)

Attributes and properties are analogous features (specifically in their usage) and have separate ways to be retrieved, set, and listened to by “value-changed signals”. This makes GetPropertyChangedSignal not useful for attributes.