Make the sprite movement system responsive upon first movement

Hi there,
I wanted to solve a problem that has been driving my crazy for the past few days.
While the script works just fine, but upon first moving it doesn’t work.

LocalScript:

local SpriteID = {
	Player = {"rbxassetid://12473304095",
		Walk = {
			Straight = {
				"rbxassetid://14706687208", -- Front row
				"rbxassetid://4733262934", -- Back row
				"rbxassetid://8819371347", -- Left row
				"rbxassetid://8819373521" -- Right row
			},
			Diagonal = {
				"rbxassetid://12443159867", -- F-L row
				"rbxassetid://3641063562", -- F-R row
				"rbxassetid://3641068326", -- B-R row
				"rbxassetid://12321281333" -- B-L row
			}
		},
		Dead = {"rbxassetid://11719986277"}
	}
}

local Players = game.Players

local plr = Players.LocalPlayer.Character
local hum = plr:FindFirstChild("Humanoid")
local actChar = plr:FindFirstChild("Character_" .. Players.LocalPlayer.Name)

hum.JumpPower = 0

local function setSpriteBasedOnMovement(moveDir)
	-- Straight
	if moveDir == Vector3.new(0, 0, 1) then
		actChar.Beam.Texture = SpriteID.Player.Walk.Straight[1]
	elseif moveDir == Vector3.new(0, 0, -1) then
		actChar.Beam.Texture = SpriteID.Player.Walk.Straight[2]
	elseif moveDir == Vector3.new(-1, 0, 0) then
		actChar.Beam.Texture = SpriteID.Player.Walk.Straight[3]
	elseif moveDir == Vector3.new(1, 0, 0) then
		actChar.Beam.Texture = SpriteID.Player.Walk.Straight[4]
		-- Diagonal
	elseif math.abs(moveDir.x - 0.707) < 0.05 and math.abs(moveDir.z - 0.707) < 0.05 then
		actChar.Beam.Texture = SpriteID.Player.Walk.Diagonal[1]
	elseif math.abs(moveDir.x + 0.707) < 0.05 and math.abs(moveDir.z - 0.707) < 0.05 then
		actChar.Beam.Texture = SpriteID.Player.Walk.Diagonal[2]
	elseif math.abs(moveDir.x - 0.707) < 0.05 and math.abs(moveDir.z + 0.707) < 0.05 then
		actChar.Beam.Texture = SpriteID.Player.Walk.Diagonal[3]
	elseif math.abs(moveDir.x + 0.707) < 0.05 and math.abs(moveDir.z + 0.707) < 0.05 then
		actChar.Beam.Texture = SpriteID.Player.Walk.Diagonal[4]
	end
end

local function setDeathSprite()
	task.spawn(function()
		while true do
			task.wait(os.clock())
			actChar.Attachment1.CFrame = CFrame.new(0, 2.5, 0)
			actChar.Beam.Texture = SpriteID.Player.Dead[1]
		end
	end)
end

if plr and hum and actChar then
	hum.Running:Connect(function(speed)
		if speed > 0.75 then
			hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
				setSpriteBasedOnMovement(hum.MoveDirection)
			end)
		else
			actChar.Beam.Texture = SpriteID.Player[1]
		end
	end)

	hum.Died:Connect(function()
		setDeathSprite()
	end)
end

MainScript (to make it work):

local SpriteID = {
	Player = {"rbxassetid://12473304095"} -- useless piece of server stuff
}

local Players = game.Players
local RS = game.ReplicatedStorage

local function configChar(plr, char)
	char:FindFirstChild("Animate").Disabled = true

	local HRP = char:FindFirstChild("HumanoidRootPart")
	local Humanoid = char:FindFirstChild("Humanoid")
	local ActualCharacter = script.Character:Clone()

	ActualCharacter.Beam.Texture = SpriteID.Player[1]
	ActualCharacter.Name = "Character_" .. plr.Name
	ActualCharacter.Parent = char
	ActualCharacter.Position = HRP.Position

	local Weld = Instance.new("Weld", ActualCharacter)
	Weld.Part0 = ActualCharacter
	Weld.Part1 = HRP
	Weld.C0 = CFrame.new(0, 2.5, 0)

	if Humanoid and ActualCharacter then
		task.spawn(function()
			for _, bodyPart in pairs(char:GetDescendants()) do
				if bodyPart:IsA("Part") then
					bodyPart.Transparency = 1
				end
			end
		end)

		Humanoid.JumpPower = 0

		local Gyro = Instance.new("BodyGyro", HRP)
		Gyro.CFrame = HRP.CFrame * CFrame.Angles(0,0,0)
		Gyro.P = 100000
		Gyro.MaxTorque = Vector3.new(400000,400000,400000)
	end
end

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		configChar(plr, char)
	end)
end)

Also, here’s an image on where they should belong (please ignore other stuff such as MainModule and SpawnLocation):
image

Thank you for your time.

1 Like